Handle constant fp classifications in fold-const-call.c
[official-gcc.git] / gcc / dwarf2out.c
blob48c22082b440f8aaa824b2670049141cdb169e9c
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 tree innerdecl;
5277 innerdecl
5278 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5279 if (!DECL_P (innerdecl)
5280 || DECL_IGNORED_P (innerdecl)
5281 || TREE_STATIC (innerdecl)
5282 || bitsize <= 0
5283 || bitpos + bitsize > 256
5284 || bitsize != maxsize)
5285 return NULL;
5286 decl = innerdecl;
5290 decl_id = DECL_UID (decl);
5291 var_loc_list **slot
5292 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5293 if (*slot == NULL)
5295 temp = ggc_cleared_alloc<var_loc_list> ();
5296 temp->decl_id = decl_id;
5297 *slot = temp;
5299 else
5300 temp = *slot;
5302 /* For PARM_DECLs try to keep around the original incoming value,
5303 even if that means we'll emit a zero-range .debug_loc entry. */
5304 if (temp->last
5305 && temp->first == temp->last
5306 && TREE_CODE (decl) == PARM_DECL
5307 && NOTE_P (temp->first->loc)
5308 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5309 && DECL_INCOMING_RTL (decl)
5310 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5311 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5312 == GET_CODE (DECL_INCOMING_RTL (decl))
5313 && prev_real_insn (temp->first->loc) == NULL_RTX
5314 && (bitsize != -1
5315 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5316 NOTE_VAR_LOCATION_LOC (loc_note))
5317 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5318 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5320 loc = ggc_cleared_alloc<var_loc_node> ();
5321 temp->first->next = loc;
5322 temp->last = loc;
5323 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5325 else if (temp->last)
5327 struct var_loc_node *last = temp->last, *unused = NULL;
5328 rtx *piece_loc = NULL, last_loc_note;
5329 HOST_WIDE_INT piece_bitpos = 0;
5330 if (last->next)
5332 last = last->next;
5333 gcc_assert (last->next == NULL);
5335 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5337 piece_loc = &last->loc;
5340 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5341 if (piece_bitpos + cur_bitsize > bitpos)
5342 break;
5343 piece_bitpos += cur_bitsize;
5344 piece_loc = &XEXP (*piece_loc, 1);
5346 while (*piece_loc);
5348 /* TEMP->LAST here is either pointer to the last but one or
5349 last element in the chained list, LAST is pointer to the
5350 last element. */
5351 if (label && strcmp (last->label, label) == 0)
5353 /* For SRA optimized variables if there weren't any real
5354 insns since last note, just modify the last node. */
5355 if (piece_loc != NULL)
5357 adjust_piece_list (piece_loc, NULL, NULL,
5358 bitpos, piece_bitpos, bitsize, loc_note);
5359 return NULL;
5361 /* If the last note doesn't cover any instructions, remove it. */
5362 if (temp->last != last)
5364 temp->last->next = NULL;
5365 unused = last;
5366 last = temp->last;
5367 gcc_assert (strcmp (last->label, label) != 0);
5369 else
5371 gcc_assert (temp->first == temp->last
5372 || (temp->first->next == temp->last
5373 && TREE_CODE (decl) == PARM_DECL));
5374 memset (temp->last, '\0', sizeof (*temp->last));
5375 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5376 return temp->last;
5379 if (bitsize == -1 && NOTE_P (last->loc))
5380 last_loc_note = last->loc;
5381 else if (piece_loc != NULL
5382 && *piece_loc != NULL_RTX
5383 && piece_bitpos == bitpos
5384 && decl_piece_bitsize (*piece_loc) == bitsize)
5385 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5386 else
5387 last_loc_note = NULL_RTX;
5388 /* If the current location is the same as the end of the list,
5389 and either both or neither of the locations is uninitialized,
5390 we have nothing to do. */
5391 if (last_loc_note == NULL_RTX
5392 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5393 NOTE_VAR_LOCATION_LOC (loc_note)))
5394 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5395 != NOTE_VAR_LOCATION_STATUS (loc_note))
5396 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5397 == VAR_INIT_STATUS_UNINITIALIZED)
5398 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5399 == VAR_INIT_STATUS_UNINITIALIZED))))
5401 /* Add LOC to the end of list and update LAST. If the last
5402 element of the list has been removed above, reuse its
5403 memory for the new node, otherwise allocate a new one. */
5404 if (unused)
5406 loc = unused;
5407 memset (loc, '\0', sizeof (*loc));
5409 else
5410 loc = ggc_cleared_alloc<var_loc_node> ();
5411 if (bitsize == -1 || piece_loc == NULL)
5412 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5413 else
5414 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5415 bitpos, piece_bitpos, bitsize, loc_note);
5416 last->next = loc;
5417 /* Ensure TEMP->LAST will point either to the new last but one
5418 element of the chain, or to the last element in it. */
5419 if (last != temp->last)
5420 temp->last = last;
5422 else if (unused)
5423 ggc_free (unused);
5425 else
5427 loc = ggc_cleared_alloc<var_loc_node> ();
5428 temp->first = loc;
5429 temp->last = loc;
5430 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5432 return loc;
5435 /* Keep track of the number of spaces used to indent the
5436 output of the debugging routines that print the structure of
5437 the DIE internal representation. */
5438 static int print_indent;
5440 /* Indent the line the number of spaces given by print_indent. */
5442 static inline void
5443 print_spaces (FILE *outfile)
5445 fprintf (outfile, "%*s", print_indent, "");
5448 /* Print a type signature in hex. */
5450 static inline void
5451 print_signature (FILE *outfile, char *sig)
5453 int i;
5455 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5456 fprintf (outfile, "%02x", sig[i] & 0xff);
5459 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5461 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5462 RECURSE, output location descriptor operations. */
5464 static void
5465 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5467 switch (val->val_class)
5469 case dw_val_class_addr:
5470 fprintf (outfile, "address");
5471 break;
5472 case dw_val_class_offset:
5473 fprintf (outfile, "offset");
5474 break;
5475 case dw_val_class_loc:
5476 fprintf (outfile, "location descriptor");
5477 if (val->v.val_loc == NULL)
5478 fprintf (outfile, " -> <null>\n");
5479 else if (recurse)
5481 fprintf (outfile, ":\n");
5482 print_indent += 4;
5483 print_loc_descr (val->v.val_loc, outfile);
5484 print_indent -= 4;
5486 else
5487 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5488 break;
5489 case dw_val_class_loc_list:
5490 fprintf (outfile, "location list -> label:%s",
5491 val->v.val_loc_list->ll_symbol);
5492 break;
5493 case dw_val_class_range_list:
5494 fprintf (outfile, "range list");
5495 break;
5496 case dw_val_class_const:
5497 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5498 break;
5499 case dw_val_class_unsigned_const:
5500 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5501 break;
5502 case dw_val_class_const_double:
5503 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5504 HOST_WIDE_INT_PRINT_UNSIGNED")",
5505 val->v.val_double.high,
5506 val->v.val_double.low);
5507 break;
5508 case dw_val_class_wide_int:
5510 int i = val->v.val_wide->get_len ();
5511 fprintf (outfile, "constant (");
5512 gcc_assert (i > 0);
5513 if (val->v.val_wide->elt (i - 1) == 0)
5514 fprintf (outfile, "0x");
5515 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5516 val->v.val_wide->elt (--i));
5517 while (--i >= 0)
5518 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5519 val->v.val_wide->elt (i));
5520 fprintf (outfile, ")");
5521 break;
5523 case dw_val_class_vec:
5524 fprintf (outfile, "floating-point or vector constant");
5525 break;
5526 case dw_val_class_flag:
5527 fprintf (outfile, "%u", val->v.val_flag);
5528 break;
5529 case dw_val_class_die_ref:
5530 if (val->v.val_die_ref.die != NULL)
5532 dw_die_ref die = val->v.val_die_ref.die;
5534 if (die->comdat_type_p)
5536 fprintf (outfile, "die -> signature: ");
5537 print_signature (outfile,
5538 die->die_id.die_type_node->signature);
5540 else if (die->die_id.die_symbol)
5541 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5542 else
5543 fprintf (outfile, "die -> %ld", die->die_offset);
5544 fprintf (outfile, " (%p)", (void *) die);
5546 else
5547 fprintf (outfile, "die -> <null>");
5548 break;
5549 case dw_val_class_vms_delta:
5550 fprintf (outfile, "delta: @slotcount(%s-%s)",
5551 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5552 break;
5553 case dw_val_class_lbl_id:
5554 case dw_val_class_lineptr:
5555 case dw_val_class_macptr:
5556 case dw_val_class_high_pc:
5557 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5558 break;
5559 case dw_val_class_str:
5560 if (val->v.val_str->str != NULL)
5561 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5562 else
5563 fprintf (outfile, "<null>");
5564 break;
5565 case dw_val_class_file:
5566 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5567 val->v.val_file->emitted_number);
5568 break;
5569 case dw_val_class_data8:
5571 int i;
5573 for (i = 0; i < 8; i++)
5574 fprintf (outfile, "%02x", val->v.val_data8[i]);
5575 break;
5577 default:
5578 break;
5582 /* Likewise, for a DIE attribute. */
5584 static void
5585 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
5587 print_dw_val (&a->dw_attr_val, recurse, outfile);
5591 /* Print the list of operands in the LOC location description to OUTFILE. This
5592 routine is a debugging aid only. */
5594 static void
5595 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5597 dw_loc_descr_ref l = loc;
5599 if (loc == NULL)
5601 print_spaces (outfile);
5602 fprintf (outfile, "<null>\n");
5603 return;
5606 for (l = loc; l != NULL; l = l->dw_loc_next)
5608 print_spaces (outfile);
5609 fprintf (outfile, "(%p) %s",
5610 (void *) l,
5611 dwarf_stack_op_name (l->dw_loc_opc));
5612 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5614 fprintf (outfile, " ");
5615 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5617 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5619 fprintf (outfile, ", ");
5620 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5622 fprintf (outfile, "\n");
5626 /* Print the information associated with a given DIE, and its children.
5627 This routine is a debugging aid only. */
5629 static void
5630 print_die (dw_die_ref die, FILE *outfile)
5632 dw_attr_node *a;
5633 dw_die_ref c;
5634 unsigned ix;
5636 print_spaces (outfile);
5637 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5638 die->die_offset, dwarf_tag_name (die->die_tag),
5639 (void*) die);
5640 print_spaces (outfile);
5641 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5642 fprintf (outfile, " offset: %ld", die->die_offset);
5643 fprintf (outfile, " mark: %d\n", die->die_mark);
5645 if (die->comdat_type_p)
5647 print_spaces (outfile);
5648 fprintf (outfile, " signature: ");
5649 print_signature (outfile, die->die_id.die_type_node->signature);
5650 fprintf (outfile, "\n");
5653 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5655 print_spaces (outfile);
5656 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5658 print_attribute (a, true, outfile);
5659 fprintf (outfile, "\n");
5662 if (die->die_child != NULL)
5664 print_indent += 4;
5665 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5666 print_indent -= 4;
5668 if (print_indent == 0)
5669 fprintf (outfile, "\n");
5672 /* Print the list of operations in the LOC location description. */
5674 DEBUG_FUNCTION void
5675 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5677 print_loc_descr (loc, stderr);
5680 /* Print the information collected for a given DIE. */
5682 DEBUG_FUNCTION void
5683 debug_dwarf_die (dw_die_ref die)
5685 print_die (die, stderr);
5688 DEBUG_FUNCTION void
5689 debug (die_struct &ref)
5691 print_die (&ref, stderr);
5694 DEBUG_FUNCTION void
5695 debug (die_struct *ptr)
5697 if (ptr)
5698 debug (*ptr);
5699 else
5700 fprintf (stderr, "<nil>\n");
5704 /* Print all DWARF information collected for the compilation unit.
5705 This routine is a debugging aid only. */
5707 DEBUG_FUNCTION void
5708 debug_dwarf (void)
5710 print_indent = 0;
5711 print_die (comp_unit_die (), stderr);
5714 /* Sanity checks on DIEs. */
5716 static void
5717 check_die (dw_die_ref die)
5719 unsigned ix;
5720 dw_attr_node *a;
5721 bool inline_found = false;
5722 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
5723 int n_decl_line = 0, n_decl_file = 0;
5724 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5726 switch (a->dw_attr)
5728 case DW_AT_inline:
5729 if (a->dw_attr_val.v.val_unsigned)
5730 inline_found = true;
5731 break;
5732 case DW_AT_location:
5733 ++n_location;
5734 break;
5735 case DW_AT_low_pc:
5736 ++n_low_pc;
5737 break;
5738 case DW_AT_high_pc:
5739 ++n_high_pc;
5740 break;
5741 case DW_AT_artificial:
5742 ++n_artificial;
5743 break;
5744 case DW_AT_decl_line:
5745 ++n_decl_line;
5746 break;
5747 case DW_AT_decl_file:
5748 ++n_decl_file;
5749 break;
5750 default:
5751 break;
5754 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
5755 || n_decl_line > 1 || n_decl_file > 1)
5757 fprintf (stderr, "Duplicate attributes in DIE:\n");
5758 debug_dwarf_die (die);
5759 gcc_unreachable ();
5761 if (inline_found)
5763 /* A debugging information entry that is a member of an abstract
5764 instance tree [that has DW_AT_inline] should not contain any
5765 attributes which describe aspects of the subroutine which vary
5766 between distinct inlined expansions or distinct out-of-line
5767 expansions. */
5768 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5769 gcc_assert (a->dw_attr != DW_AT_low_pc
5770 && a->dw_attr != DW_AT_high_pc
5771 && a->dw_attr != DW_AT_location
5772 && a->dw_attr != DW_AT_frame_base
5773 && a->dw_attr != DW_AT_GNU_all_call_sites);
5777 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5778 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5779 DIE that marks the start of the DIEs for this include file. */
5781 static dw_die_ref
5782 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5784 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5785 dw_die_ref new_unit = gen_compile_unit_die (filename);
5787 new_unit->die_sib = old_unit;
5788 return new_unit;
5791 /* Close an include-file CU and reopen the enclosing one. */
5793 static dw_die_ref
5794 pop_compile_unit (dw_die_ref old_unit)
5796 dw_die_ref new_unit = old_unit->die_sib;
5798 old_unit->die_sib = NULL;
5799 return new_unit;
5802 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5803 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5804 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5806 /* Calculate the checksum of a location expression. */
5808 static inline void
5809 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5811 int tem;
5812 inchash::hash hstate;
5813 hashval_t hash;
5815 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5816 CHECKSUM (tem);
5817 hash_loc_operands (loc, hstate);
5818 hash = hstate.end();
5819 CHECKSUM (hash);
5822 /* Calculate the checksum of an attribute. */
5824 static void
5825 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
5827 dw_loc_descr_ref loc;
5828 rtx r;
5830 CHECKSUM (at->dw_attr);
5832 /* We don't care that this was compiled with a different compiler
5833 snapshot; if the output is the same, that's what matters. */
5834 if (at->dw_attr == DW_AT_producer)
5835 return;
5837 switch (AT_class (at))
5839 case dw_val_class_const:
5840 CHECKSUM (at->dw_attr_val.v.val_int);
5841 break;
5842 case dw_val_class_unsigned_const:
5843 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5844 break;
5845 case dw_val_class_const_double:
5846 CHECKSUM (at->dw_attr_val.v.val_double);
5847 break;
5848 case dw_val_class_wide_int:
5849 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
5850 get_full_len (*at->dw_attr_val.v.val_wide)
5851 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
5852 break;
5853 case dw_val_class_vec:
5854 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5855 (at->dw_attr_val.v.val_vec.length
5856 * at->dw_attr_val.v.val_vec.elt_size));
5857 break;
5858 case dw_val_class_flag:
5859 CHECKSUM (at->dw_attr_val.v.val_flag);
5860 break;
5861 case dw_val_class_str:
5862 CHECKSUM_STRING (AT_string (at));
5863 break;
5865 case dw_val_class_addr:
5866 r = AT_addr (at);
5867 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5868 CHECKSUM_STRING (XSTR (r, 0));
5869 break;
5871 case dw_val_class_offset:
5872 CHECKSUM (at->dw_attr_val.v.val_offset);
5873 break;
5875 case dw_val_class_loc:
5876 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5877 loc_checksum (loc, ctx);
5878 break;
5880 case dw_val_class_die_ref:
5881 die_checksum (AT_ref (at), ctx, mark);
5882 break;
5884 case dw_val_class_fde_ref:
5885 case dw_val_class_vms_delta:
5886 case dw_val_class_lbl_id:
5887 case dw_val_class_lineptr:
5888 case dw_val_class_macptr:
5889 case dw_val_class_high_pc:
5890 break;
5892 case dw_val_class_file:
5893 CHECKSUM_STRING (AT_file (at)->filename);
5894 break;
5896 case dw_val_class_data8:
5897 CHECKSUM (at->dw_attr_val.v.val_data8);
5898 break;
5900 default:
5901 break;
5905 /* Calculate the checksum of a DIE. */
5907 static void
5908 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5910 dw_die_ref c;
5911 dw_attr_node *a;
5912 unsigned ix;
5914 /* To avoid infinite recursion. */
5915 if (die->die_mark)
5917 CHECKSUM (die->die_mark);
5918 return;
5920 die->die_mark = ++(*mark);
5922 CHECKSUM (die->die_tag);
5924 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5925 attr_checksum (a, ctx, mark);
5927 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5930 #undef CHECKSUM
5931 #undef CHECKSUM_BLOCK
5932 #undef CHECKSUM_STRING
5934 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5935 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5936 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5937 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5938 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5939 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5940 #define CHECKSUM_ATTR(FOO) \
5941 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5943 /* Calculate the checksum of a number in signed LEB128 format. */
5945 static void
5946 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5948 unsigned char byte;
5949 bool more;
5951 while (1)
5953 byte = (value & 0x7f);
5954 value >>= 7;
5955 more = !((value == 0 && (byte & 0x40) == 0)
5956 || (value == -1 && (byte & 0x40) != 0));
5957 if (more)
5958 byte |= 0x80;
5959 CHECKSUM (byte);
5960 if (!more)
5961 break;
5965 /* Calculate the checksum of a number in unsigned LEB128 format. */
5967 static void
5968 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5970 while (1)
5972 unsigned char byte = (value & 0x7f);
5973 value >>= 7;
5974 if (value != 0)
5975 /* More bytes to follow. */
5976 byte |= 0x80;
5977 CHECKSUM (byte);
5978 if (value == 0)
5979 break;
5983 /* Checksum the context of the DIE. This adds the names of any
5984 surrounding namespaces or structures to the checksum. */
5986 static void
5987 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5989 const char *name;
5990 dw_die_ref spec;
5991 int tag = die->die_tag;
5993 if (tag != DW_TAG_namespace
5994 && tag != DW_TAG_structure_type
5995 && tag != DW_TAG_class_type)
5996 return;
5998 name = get_AT_string (die, DW_AT_name);
6000 spec = get_AT_ref (die, DW_AT_specification);
6001 if (spec != NULL)
6002 die = spec;
6004 if (die->die_parent != NULL)
6005 checksum_die_context (die->die_parent, ctx);
6007 CHECKSUM_ULEB128 ('C');
6008 CHECKSUM_ULEB128 (tag);
6009 if (name != NULL)
6010 CHECKSUM_STRING (name);
6013 /* Calculate the checksum of a location expression. */
6015 static inline void
6016 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6018 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6019 were emitted as a DW_FORM_sdata instead of a location expression. */
6020 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6022 CHECKSUM_ULEB128 (DW_FORM_sdata);
6023 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6024 return;
6027 /* Otherwise, just checksum the raw location expression. */
6028 while (loc != NULL)
6030 inchash::hash hstate;
6031 hashval_t hash;
6033 CHECKSUM_ULEB128 (loc->dtprel);
6034 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6035 hash_loc_operands (loc, hstate);
6036 hash = hstate.end ();
6037 CHECKSUM (hash);
6038 loc = loc->dw_loc_next;
6042 /* Calculate the checksum of an attribute. */
6044 static void
6045 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6046 struct md5_ctx *ctx, int *mark)
6048 dw_loc_descr_ref loc;
6049 rtx r;
6051 if (AT_class (at) == dw_val_class_die_ref)
6053 dw_die_ref target_die = AT_ref (at);
6055 /* For pointer and reference types, we checksum only the (qualified)
6056 name of the target type (if there is a name). For friend entries,
6057 we checksum only the (qualified) name of the target type or function.
6058 This allows the checksum to remain the same whether the target type
6059 is complete or not. */
6060 if ((at->dw_attr == DW_AT_type
6061 && (tag == DW_TAG_pointer_type
6062 || tag == DW_TAG_reference_type
6063 || tag == DW_TAG_rvalue_reference_type
6064 || tag == DW_TAG_ptr_to_member_type))
6065 || (at->dw_attr == DW_AT_friend
6066 && tag == DW_TAG_friend))
6068 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6070 if (name_attr != NULL)
6072 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6074 if (decl == NULL)
6075 decl = target_die;
6076 CHECKSUM_ULEB128 ('N');
6077 CHECKSUM_ULEB128 (at->dw_attr);
6078 if (decl->die_parent != NULL)
6079 checksum_die_context (decl->die_parent, ctx);
6080 CHECKSUM_ULEB128 ('E');
6081 CHECKSUM_STRING (AT_string (name_attr));
6082 return;
6086 /* For all other references to another DIE, we check to see if the
6087 target DIE has already been visited. If it has, we emit a
6088 backward reference; if not, we descend recursively. */
6089 if (target_die->die_mark > 0)
6091 CHECKSUM_ULEB128 ('R');
6092 CHECKSUM_ULEB128 (at->dw_attr);
6093 CHECKSUM_ULEB128 (target_die->die_mark);
6095 else
6097 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6099 if (decl == NULL)
6100 decl = target_die;
6101 target_die->die_mark = ++(*mark);
6102 CHECKSUM_ULEB128 ('T');
6103 CHECKSUM_ULEB128 (at->dw_attr);
6104 if (decl->die_parent != NULL)
6105 checksum_die_context (decl->die_parent, ctx);
6106 die_checksum_ordered (target_die, ctx, mark);
6108 return;
6111 CHECKSUM_ULEB128 ('A');
6112 CHECKSUM_ULEB128 (at->dw_attr);
6114 switch (AT_class (at))
6116 case dw_val_class_const:
6117 CHECKSUM_ULEB128 (DW_FORM_sdata);
6118 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6119 break;
6121 case dw_val_class_unsigned_const:
6122 CHECKSUM_ULEB128 (DW_FORM_sdata);
6123 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6124 break;
6126 case dw_val_class_const_double:
6127 CHECKSUM_ULEB128 (DW_FORM_block);
6128 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6129 CHECKSUM (at->dw_attr_val.v.val_double);
6130 break;
6132 case dw_val_class_wide_int:
6133 CHECKSUM_ULEB128 (DW_FORM_block);
6134 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6135 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6136 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6137 get_full_len (*at->dw_attr_val.v.val_wide)
6138 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6139 break;
6141 case dw_val_class_vec:
6142 CHECKSUM_ULEB128 (DW_FORM_block);
6143 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6144 * at->dw_attr_val.v.val_vec.elt_size);
6145 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6146 (at->dw_attr_val.v.val_vec.length
6147 * at->dw_attr_val.v.val_vec.elt_size));
6148 break;
6150 case dw_val_class_flag:
6151 CHECKSUM_ULEB128 (DW_FORM_flag);
6152 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6153 break;
6155 case dw_val_class_str:
6156 CHECKSUM_ULEB128 (DW_FORM_string);
6157 CHECKSUM_STRING (AT_string (at));
6158 break;
6160 case dw_val_class_addr:
6161 r = AT_addr (at);
6162 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6163 CHECKSUM_ULEB128 (DW_FORM_string);
6164 CHECKSUM_STRING (XSTR (r, 0));
6165 break;
6167 case dw_val_class_offset:
6168 CHECKSUM_ULEB128 (DW_FORM_sdata);
6169 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6170 break;
6172 case dw_val_class_loc:
6173 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6174 loc_checksum_ordered (loc, ctx);
6175 break;
6177 case dw_val_class_fde_ref:
6178 case dw_val_class_lbl_id:
6179 case dw_val_class_lineptr:
6180 case dw_val_class_macptr:
6181 case dw_val_class_high_pc:
6182 break;
6184 case dw_val_class_file:
6185 CHECKSUM_ULEB128 (DW_FORM_string);
6186 CHECKSUM_STRING (AT_file (at)->filename);
6187 break;
6189 case dw_val_class_data8:
6190 CHECKSUM (at->dw_attr_val.v.val_data8);
6191 break;
6193 default:
6194 break;
6198 struct checksum_attributes
6200 dw_attr_node *at_name;
6201 dw_attr_node *at_type;
6202 dw_attr_node *at_friend;
6203 dw_attr_node *at_accessibility;
6204 dw_attr_node *at_address_class;
6205 dw_attr_node *at_allocated;
6206 dw_attr_node *at_artificial;
6207 dw_attr_node *at_associated;
6208 dw_attr_node *at_binary_scale;
6209 dw_attr_node *at_bit_offset;
6210 dw_attr_node *at_bit_size;
6211 dw_attr_node *at_bit_stride;
6212 dw_attr_node *at_byte_size;
6213 dw_attr_node *at_byte_stride;
6214 dw_attr_node *at_const_value;
6215 dw_attr_node *at_containing_type;
6216 dw_attr_node *at_count;
6217 dw_attr_node *at_data_location;
6218 dw_attr_node *at_data_member_location;
6219 dw_attr_node *at_decimal_scale;
6220 dw_attr_node *at_decimal_sign;
6221 dw_attr_node *at_default_value;
6222 dw_attr_node *at_digit_count;
6223 dw_attr_node *at_discr;
6224 dw_attr_node *at_discr_list;
6225 dw_attr_node *at_discr_value;
6226 dw_attr_node *at_encoding;
6227 dw_attr_node *at_endianity;
6228 dw_attr_node *at_explicit;
6229 dw_attr_node *at_is_optional;
6230 dw_attr_node *at_location;
6231 dw_attr_node *at_lower_bound;
6232 dw_attr_node *at_mutable;
6233 dw_attr_node *at_ordering;
6234 dw_attr_node *at_picture_string;
6235 dw_attr_node *at_prototyped;
6236 dw_attr_node *at_small;
6237 dw_attr_node *at_segment;
6238 dw_attr_node *at_string_length;
6239 dw_attr_node *at_threads_scaled;
6240 dw_attr_node *at_upper_bound;
6241 dw_attr_node *at_use_location;
6242 dw_attr_node *at_use_UTF8;
6243 dw_attr_node *at_variable_parameter;
6244 dw_attr_node *at_virtuality;
6245 dw_attr_node *at_visibility;
6246 dw_attr_node *at_vtable_elem_location;
6249 /* Collect the attributes that we will want to use for the checksum. */
6251 static void
6252 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6254 dw_attr_node *a;
6255 unsigned ix;
6257 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6259 switch (a->dw_attr)
6261 case DW_AT_name:
6262 attrs->at_name = a;
6263 break;
6264 case DW_AT_type:
6265 attrs->at_type = a;
6266 break;
6267 case DW_AT_friend:
6268 attrs->at_friend = a;
6269 break;
6270 case DW_AT_accessibility:
6271 attrs->at_accessibility = a;
6272 break;
6273 case DW_AT_address_class:
6274 attrs->at_address_class = a;
6275 break;
6276 case DW_AT_allocated:
6277 attrs->at_allocated = a;
6278 break;
6279 case DW_AT_artificial:
6280 attrs->at_artificial = a;
6281 break;
6282 case DW_AT_associated:
6283 attrs->at_associated = a;
6284 break;
6285 case DW_AT_binary_scale:
6286 attrs->at_binary_scale = a;
6287 break;
6288 case DW_AT_bit_offset:
6289 attrs->at_bit_offset = a;
6290 break;
6291 case DW_AT_bit_size:
6292 attrs->at_bit_size = a;
6293 break;
6294 case DW_AT_bit_stride:
6295 attrs->at_bit_stride = a;
6296 break;
6297 case DW_AT_byte_size:
6298 attrs->at_byte_size = a;
6299 break;
6300 case DW_AT_byte_stride:
6301 attrs->at_byte_stride = a;
6302 break;
6303 case DW_AT_const_value:
6304 attrs->at_const_value = a;
6305 break;
6306 case DW_AT_containing_type:
6307 attrs->at_containing_type = a;
6308 break;
6309 case DW_AT_count:
6310 attrs->at_count = a;
6311 break;
6312 case DW_AT_data_location:
6313 attrs->at_data_location = a;
6314 break;
6315 case DW_AT_data_member_location:
6316 attrs->at_data_member_location = a;
6317 break;
6318 case DW_AT_decimal_scale:
6319 attrs->at_decimal_scale = a;
6320 break;
6321 case DW_AT_decimal_sign:
6322 attrs->at_decimal_sign = a;
6323 break;
6324 case DW_AT_default_value:
6325 attrs->at_default_value = a;
6326 break;
6327 case DW_AT_digit_count:
6328 attrs->at_digit_count = a;
6329 break;
6330 case DW_AT_discr:
6331 attrs->at_discr = a;
6332 break;
6333 case DW_AT_discr_list:
6334 attrs->at_discr_list = a;
6335 break;
6336 case DW_AT_discr_value:
6337 attrs->at_discr_value = a;
6338 break;
6339 case DW_AT_encoding:
6340 attrs->at_encoding = a;
6341 break;
6342 case DW_AT_endianity:
6343 attrs->at_endianity = a;
6344 break;
6345 case DW_AT_explicit:
6346 attrs->at_explicit = a;
6347 break;
6348 case DW_AT_is_optional:
6349 attrs->at_is_optional = a;
6350 break;
6351 case DW_AT_location:
6352 attrs->at_location = a;
6353 break;
6354 case DW_AT_lower_bound:
6355 attrs->at_lower_bound = a;
6356 break;
6357 case DW_AT_mutable:
6358 attrs->at_mutable = a;
6359 break;
6360 case DW_AT_ordering:
6361 attrs->at_ordering = a;
6362 break;
6363 case DW_AT_picture_string:
6364 attrs->at_picture_string = a;
6365 break;
6366 case DW_AT_prototyped:
6367 attrs->at_prototyped = a;
6368 break;
6369 case DW_AT_small:
6370 attrs->at_small = a;
6371 break;
6372 case DW_AT_segment:
6373 attrs->at_segment = a;
6374 break;
6375 case DW_AT_string_length:
6376 attrs->at_string_length = a;
6377 break;
6378 case DW_AT_threads_scaled:
6379 attrs->at_threads_scaled = a;
6380 break;
6381 case DW_AT_upper_bound:
6382 attrs->at_upper_bound = a;
6383 break;
6384 case DW_AT_use_location:
6385 attrs->at_use_location = a;
6386 break;
6387 case DW_AT_use_UTF8:
6388 attrs->at_use_UTF8 = a;
6389 break;
6390 case DW_AT_variable_parameter:
6391 attrs->at_variable_parameter = a;
6392 break;
6393 case DW_AT_virtuality:
6394 attrs->at_virtuality = a;
6395 break;
6396 case DW_AT_visibility:
6397 attrs->at_visibility = a;
6398 break;
6399 case DW_AT_vtable_elem_location:
6400 attrs->at_vtable_elem_location = a;
6401 break;
6402 default:
6403 break;
6408 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6410 static void
6411 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6413 dw_die_ref c;
6414 dw_die_ref decl;
6415 struct checksum_attributes attrs;
6417 CHECKSUM_ULEB128 ('D');
6418 CHECKSUM_ULEB128 (die->die_tag);
6420 memset (&attrs, 0, sizeof (attrs));
6422 decl = get_AT_ref (die, DW_AT_specification);
6423 if (decl != NULL)
6424 collect_checksum_attributes (&attrs, decl);
6425 collect_checksum_attributes (&attrs, die);
6427 CHECKSUM_ATTR (attrs.at_name);
6428 CHECKSUM_ATTR (attrs.at_accessibility);
6429 CHECKSUM_ATTR (attrs.at_address_class);
6430 CHECKSUM_ATTR (attrs.at_allocated);
6431 CHECKSUM_ATTR (attrs.at_artificial);
6432 CHECKSUM_ATTR (attrs.at_associated);
6433 CHECKSUM_ATTR (attrs.at_binary_scale);
6434 CHECKSUM_ATTR (attrs.at_bit_offset);
6435 CHECKSUM_ATTR (attrs.at_bit_size);
6436 CHECKSUM_ATTR (attrs.at_bit_stride);
6437 CHECKSUM_ATTR (attrs.at_byte_size);
6438 CHECKSUM_ATTR (attrs.at_byte_stride);
6439 CHECKSUM_ATTR (attrs.at_const_value);
6440 CHECKSUM_ATTR (attrs.at_containing_type);
6441 CHECKSUM_ATTR (attrs.at_count);
6442 CHECKSUM_ATTR (attrs.at_data_location);
6443 CHECKSUM_ATTR (attrs.at_data_member_location);
6444 CHECKSUM_ATTR (attrs.at_decimal_scale);
6445 CHECKSUM_ATTR (attrs.at_decimal_sign);
6446 CHECKSUM_ATTR (attrs.at_default_value);
6447 CHECKSUM_ATTR (attrs.at_digit_count);
6448 CHECKSUM_ATTR (attrs.at_discr);
6449 CHECKSUM_ATTR (attrs.at_discr_list);
6450 CHECKSUM_ATTR (attrs.at_discr_value);
6451 CHECKSUM_ATTR (attrs.at_encoding);
6452 CHECKSUM_ATTR (attrs.at_endianity);
6453 CHECKSUM_ATTR (attrs.at_explicit);
6454 CHECKSUM_ATTR (attrs.at_is_optional);
6455 CHECKSUM_ATTR (attrs.at_location);
6456 CHECKSUM_ATTR (attrs.at_lower_bound);
6457 CHECKSUM_ATTR (attrs.at_mutable);
6458 CHECKSUM_ATTR (attrs.at_ordering);
6459 CHECKSUM_ATTR (attrs.at_picture_string);
6460 CHECKSUM_ATTR (attrs.at_prototyped);
6461 CHECKSUM_ATTR (attrs.at_small);
6462 CHECKSUM_ATTR (attrs.at_segment);
6463 CHECKSUM_ATTR (attrs.at_string_length);
6464 CHECKSUM_ATTR (attrs.at_threads_scaled);
6465 CHECKSUM_ATTR (attrs.at_upper_bound);
6466 CHECKSUM_ATTR (attrs.at_use_location);
6467 CHECKSUM_ATTR (attrs.at_use_UTF8);
6468 CHECKSUM_ATTR (attrs.at_variable_parameter);
6469 CHECKSUM_ATTR (attrs.at_virtuality);
6470 CHECKSUM_ATTR (attrs.at_visibility);
6471 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6472 CHECKSUM_ATTR (attrs.at_type);
6473 CHECKSUM_ATTR (attrs.at_friend);
6475 /* Checksum the child DIEs. */
6476 c = die->die_child;
6477 if (c) do {
6478 dw_attr_node *name_attr;
6480 c = c->die_sib;
6481 name_attr = get_AT (c, DW_AT_name);
6482 if (is_template_instantiation (c))
6484 /* Ignore instantiations of member type and function templates. */
6486 else if (name_attr != NULL
6487 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6489 /* Use a shallow checksum for named nested types and member
6490 functions. */
6491 CHECKSUM_ULEB128 ('S');
6492 CHECKSUM_ULEB128 (c->die_tag);
6493 CHECKSUM_STRING (AT_string (name_attr));
6495 else
6497 /* Use a deep checksum for other children. */
6498 /* Mark this DIE so it gets processed when unmarking. */
6499 if (c->die_mark == 0)
6500 c->die_mark = -1;
6501 die_checksum_ordered (c, ctx, mark);
6503 } while (c != die->die_child);
6505 CHECKSUM_ULEB128 (0);
6508 /* Add a type name and tag to a hash. */
6509 static void
6510 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6512 CHECKSUM_ULEB128 (tag);
6513 CHECKSUM_STRING (name);
6516 #undef CHECKSUM
6517 #undef CHECKSUM_STRING
6518 #undef CHECKSUM_ATTR
6519 #undef CHECKSUM_LEB128
6520 #undef CHECKSUM_ULEB128
6522 /* Generate the type signature for DIE. This is computed by generating an
6523 MD5 checksum over the DIE's tag, its relevant attributes, and its
6524 children. Attributes that are references to other DIEs are processed
6525 by recursion, using the MARK field to prevent infinite recursion.
6526 If the DIE is nested inside a namespace or another type, we also
6527 need to include that context in the signature. The lower 64 bits
6528 of the resulting MD5 checksum comprise the signature. */
6530 static void
6531 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6533 int mark;
6534 const char *name;
6535 unsigned char checksum[16];
6536 struct md5_ctx ctx;
6537 dw_die_ref decl;
6538 dw_die_ref parent;
6540 name = get_AT_string (die, DW_AT_name);
6541 decl = get_AT_ref (die, DW_AT_specification);
6542 parent = get_die_parent (die);
6544 /* First, compute a signature for just the type name (and its surrounding
6545 context, if any. This is stored in the type unit DIE for link-time
6546 ODR (one-definition rule) checking. */
6548 if (is_cxx () && name != NULL)
6550 md5_init_ctx (&ctx);
6552 /* Checksum the names of surrounding namespaces and structures. */
6553 if (parent != NULL)
6554 checksum_die_context (parent, &ctx);
6556 /* Checksum the current DIE. */
6557 die_odr_checksum (die->die_tag, name, &ctx);
6558 md5_finish_ctx (&ctx, checksum);
6560 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6563 /* Next, compute the complete type signature. */
6565 md5_init_ctx (&ctx);
6566 mark = 1;
6567 die->die_mark = mark;
6569 /* Checksum the names of surrounding namespaces and structures. */
6570 if (parent != NULL)
6571 checksum_die_context (parent, &ctx);
6573 /* Checksum the DIE and its children. */
6574 die_checksum_ordered (die, &ctx, &mark);
6575 unmark_all_dies (die);
6576 md5_finish_ctx (&ctx, checksum);
6578 /* Store the signature in the type node and link the type DIE and the
6579 type node together. */
6580 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6581 DWARF_TYPE_SIGNATURE_SIZE);
6582 die->comdat_type_p = true;
6583 die->die_id.die_type_node = type_node;
6584 type_node->type_die = die;
6586 /* If the DIE is a specification, link its declaration to the type node
6587 as well. */
6588 if (decl != NULL)
6590 decl->comdat_type_p = true;
6591 decl->die_id.die_type_node = type_node;
6595 /* Do the location expressions look same? */
6596 static inline int
6597 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6599 return loc1->dw_loc_opc == loc2->dw_loc_opc
6600 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6601 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6604 /* Do the values look the same? */
6605 static int
6606 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6608 dw_loc_descr_ref loc1, loc2;
6609 rtx r1, r2;
6611 if (v1->val_class != v2->val_class)
6612 return 0;
6614 switch (v1->val_class)
6616 case dw_val_class_const:
6617 return v1->v.val_int == v2->v.val_int;
6618 case dw_val_class_unsigned_const:
6619 return v1->v.val_unsigned == v2->v.val_unsigned;
6620 case dw_val_class_const_double:
6621 return v1->v.val_double.high == v2->v.val_double.high
6622 && v1->v.val_double.low == v2->v.val_double.low;
6623 case dw_val_class_wide_int:
6624 return *v1->v.val_wide == *v2->v.val_wide;
6625 case dw_val_class_vec:
6626 if (v1->v.val_vec.length != v2->v.val_vec.length
6627 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6628 return 0;
6629 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6630 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6631 return 0;
6632 return 1;
6633 case dw_val_class_flag:
6634 return v1->v.val_flag == v2->v.val_flag;
6635 case dw_val_class_str:
6636 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6638 case dw_val_class_addr:
6639 r1 = v1->v.val_addr;
6640 r2 = v2->v.val_addr;
6641 if (GET_CODE (r1) != GET_CODE (r2))
6642 return 0;
6643 return !rtx_equal_p (r1, r2);
6645 case dw_val_class_offset:
6646 return v1->v.val_offset == v2->v.val_offset;
6648 case dw_val_class_loc:
6649 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6650 loc1 && loc2;
6651 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6652 if (!same_loc_p (loc1, loc2, mark))
6653 return 0;
6654 return !loc1 && !loc2;
6656 case dw_val_class_die_ref:
6657 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6659 case dw_val_class_fde_ref:
6660 case dw_val_class_vms_delta:
6661 case dw_val_class_lbl_id:
6662 case dw_val_class_lineptr:
6663 case dw_val_class_macptr:
6664 case dw_val_class_high_pc:
6665 return 1;
6667 case dw_val_class_file:
6668 return v1->v.val_file == v2->v.val_file;
6670 case dw_val_class_data8:
6671 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6673 default:
6674 return 1;
6678 /* Do the attributes look the same? */
6680 static int
6681 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
6683 if (at1->dw_attr != at2->dw_attr)
6684 return 0;
6686 /* We don't care that this was compiled with a different compiler
6687 snapshot; if the output is the same, that's what matters. */
6688 if (at1->dw_attr == DW_AT_producer)
6689 return 1;
6691 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6694 /* Do the dies look the same? */
6696 static int
6697 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6699 dw_die_ref c1, c2;
6700 dw_attr_node *a1;
6701 unsigned ix;
6703 /* To avoid infinite recursion. */
6704 if (die1->die_mark)
6705 return die1->die_mark == die2->die_mark;
6706 die1->die_mark = die2->die_mark = ++(*mark);
6708 if (die1->die_tag != die2->die_tag)
6709 return 0;
6711 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6712 return 0;
6714 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6715 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6716 return 0;
6718 c1 = die1->die_child;
6719 c2 = die2->die_child;
6720 if (! c1)
6722 if (c2)
6723 return 0;
6725 else
6726 for (;;)
6728 if (!same_die_p (c1, c2, mark))
6729 return 0;
6730 c1 = c1->die_sib;
6731 c2 = c2->die_sib;
6732 if (c1 == die1->die_child)
6734 if (c2 == die2->die_child)
6735 break;
6736 else
6737 return 0;
6741 return 1;
6744 /* Do the dies look the same? Wrapper around same_die_p. */
6746 static int
6747 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6749 int mark = 0;
6750 int ret = same_die_p (die1, die2, &mark);
6752 unmark_all_dies (die1);
6753 unmark_all_dies (die2);
6755 return ret;
6758 /* The prefix to attach to symbols on DIEs in the current comdat debug
6759 info section. */
6760 static const char *comdat_symbol_id;
6762 /* The index of the current symbol within the current comdat CU. */
6763 static unsigned int comdat_symbol_number;
6765 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6766 children, and set comdat_symbol_id accordingly. */
6768 static void
6769 compute_section_prefix (dw_die_ref unit_die)
6771 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6772 const char *base = die_name ? lbasename (die_name) : "anonymous";
6773 char *name = XALLOCAVEC (char, strlen (base) + 64);
6774 char *p;
6775 int i, mark;
6776 unsigned char checksum[16];
6777 struct md5_ctx ctx;
6779 /* Compute the checksum of the DIE, then append part of it as hex digits to
6780 the name filename of the unit. */
6782 md5_init_ctx (&ctx);
6783 mark = 0;
6784 die_checksum (unit_die, &ctx, &mark);
6785 unmark_all_dies (unit_die);
6786 md5_finish_ctx (&ctx, checksum);
6788 sprintf (name, "%s.", base);
6789 clean_symbol_name (name);
6791 p = name + strlen (name);
6792 for (i = 0; i < 4; i++)
6794 sprintf (p, "%.2x", checksum[i]);
6795 p += 2;
6798 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6799 comdat_symbol_number = 0;
6802 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6804 static int
6805 is_type_die (dw_die_ref die)
6807 switch (die->die_tag)
6809 case DW_TAG_array_type:
6810 case DW_TAG_class_type:
6811 case DW_TAG_interface_type:
6812 case DW_TAG_enumeration_type:
6813 case DW_TAG_pointer_type:
6814 case DW_TAG_reference_type:
6815 case DW_TAG_rvalue_reference_type:
6816 case DW_TAG_string_type:
6817 case DW_TAG_structure_type:
6818 case DW_TAG_subroutine_type:
6819 case DW_TAG_union_type:
6820 case DW_TAG_ptr_to_member_type:
6821 case DW_TAG_set_type:
6822 case DW_TAG_subrange_type:
6823 case DW_TAG_base_type:
6824 case DW_TAG_const_type:
6825 case DW_TAG_file_type:
6826 case DW_TAG_packed_type:
6827 case DW_TAG_volatile_type:
6828 case DW_TAG_typedef:
6829 return 1;
6830 default:
6831 return 0;
6835 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6836 Basically, we want to choose the bits that are likely to be shared between
6837 compilations (types) and leave out the bits that are specific to individual
6838 compilations (functions). */
6840 static int
6841 is_comdat_die (dw_die_ref c)
6843 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6844 we do for stabs. The advantage is a greater likelihood of sharing between
6845 objects that don't include headers in the same order (and therefore would
6846 put the base types in a different comdat). jason 8/28/00 */
6848 if (c->die_tag == DW_TAG_base_type)
6849 return 0;
6851 if (c->die_tag == DW_TAG_pointer_type
6852 || c->die_tag == DW_TAG_reference_type
6853 || c->die_tag == DW_TAG_rvalue_reference_type
6854 || c->die_tag == DW_TAG_const_type
6855 || c->die_tag == DW_TAG_volatile_type)
6857 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6859 return t ? is_comdat_die (t) : 0;
6862 return is_type_die (c);
6865 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6866 compilation unit. */
6868 static int
6869 is_symbol_die (dw_die_ref c)
6871 return (is_type_die (c)
6872 || is_declaration_die (c)
6873 || c->die_tag == DW_TAG_namespace
6874 || c->die_tag == DW_TAG_module);
6877 /* Returns true iff C is a compile-unit DIE. */
6879 static inline bool
6880 is_cu_die (dw_die_ref c)
6882 return c && c->die_tag == DW_TAG_compile_unit;
6885 /* Returns true iff C is a unit DIE of some sort. */
6887 static inline bool
6888 is_unit_die (dw_die_ref c)
6890 return c && (c->die_tag == DW_TAG_compile_unit
6891 || c->die_tag == DW_TAG_partial_unit
6892 || c->die_tag == DW_TAG_type_unit);
6895 /* Returns true iff C is a namespace DIE. */
6897 static inline bool
6898 is_namespace_die (dw_die_ref c)
6900 return c && c->die_tag == DW_TAG_namespace;
6903 /* Returns true iff C is a class or structure DIE. */
6905 static inline bool
6906 is_class_die (dw_die_ref c)
6908 return c && (c->die_tag == DW_TAG_class_type
6909 || c->die_tag == DW_TAG_structure_type);
6912 /* Return non-zero if this DIE is a template parameter. */
6914 static inline bool
6915 is_template_parameter (dw_die_ref die)
6917 switch (die->die_tag)
6919 case DW_TAG_template_type_param:
6920 case DW_TAG_template_value_param:
6921 case DW_TAG_GNU_template_template_param:
6922 case DW_TAG_GNU_template_parameter_pack:
6923 return true;
6924 default:
6925 return false;
6929 /* Return non-zero if this DIE represents a template instantiation. */
6931 static inline bool
6932 is_template_instantiation (dw_die_ref die)
6934 dw_die_ref c;
6936 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6937 return false;
6938 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6939 return false;
6942 static char *
6943 gen_internal_sym (const char *prefix)
6945 char buf[256];
6947 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6948 return xstrdup (buf);
6951 /* Assign symbols to all worthy DIEs under DIE. */
6953 static void
6954 assign_symbol_names (dw_die_ref die)
6956 dw_die_ref c;
6958 if (is_symbol_die (die) && !die->comdat_type_p)
6960 if (comdat_symbol_id)
6962 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6964 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6965 comdat_symbol_id, comdat_symbol_number++);
6966 die->die_id.die_symbol = xstrdup (p);
6968 else
6969 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6972 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6975 struct cu_hash_table_entry
6977 dw_die_ref cu;
6978 unsigned min_comdat_num, max_comdat_num;
6979 struct cu_hash_table_entry *next;
6982 /* Helpers to manipulate hash table of CUs. */
6984 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
6986 typedef die_struct *compare_type;
6987 static inline hashval_t hash (const cu_hash_table_entry *);
6988 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
6989 static inline void remove (cu_hash_table_entry *);
6992 inline hashval_t
6993 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
6995 return htab_hash_string (entry->cu->die_id.die_symbol);
6998 inline bool
6999 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
7000 const die_struct *entry2)
7002 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7005 inline void
7006 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7008 struct cu_hash_table_entry *next;
7010 while (entry)
7012 next = entry->next;
7013 free (entry);
7014 entry = next;
7018 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7020 /* Check whether we have already seen this CU and set up SYM_NUM
7021 accordingly. */
7022 static int
7023 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7025 struct cu_hash_table_entry dummy;
7026 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7028 dummy.max_comdat_num = 0;
7030 slot = htable->find_slot_with_hash (cu,
7031 htab_hash_string (cu->die_id.die_symbol),
7032 INSERT);
7033 entry = *slot;
7035 for (; entry; last = entry, entry = entry->next)
7037 if (same_die_p_wrap (cu, entry->cu))
7038 break;
7041 if (entry)
7043 *sym_num = entry->min_comdat_num;
7044 return 1;
7047 entry = XCNEW (struct cu_hash_table_entry);
7048 entry->cu = cu;
7049 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7050 entry->next = *slot;
7051 *slot = entry;
7053 return 0;
7056 /* Record SYM_NUM to record of CU in HTABLE. */
7057 static void
7058 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7059 unsigned int sym_num)
7061 struct cu_hash_table_entry **slot, *entry;
7063 slot = htable->find_slot_with_hash (cu,
7064 htab_hash_string (cu->die_id.die_symbol),
7065 NO_INSERT);
7066 entry = *slot;
7068 entry->max_comdat_num = sym_num;
7071 /* Traverse the DIE (which is always comp_unit_die), and set up
7072 additional compilation units for each of the include files we see
7073 bracketed by BINCL/EINCL. */
7075 static void
7076 break_out_includes (dw_die_ref die)
7078 dw_die_ref c;
7079 dw_die_ref unit = NULL;
7080 limbo_die_node *node, **pnode;
7082 c = die->die_child;
7083 if (c) do {
7084 dw_die_ref prev = c;
7085 c = c->die_sib;
7086 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7087 || (unit && is_comdat_die (c)))
7089 dw_die_ref next = c->die_sib;
7091 /* This DIE is for a secondary CU; remove it from the main one. */
7092 remove_child_with_prev (c, prev);
7094 if (c->die_tag == DW_TAG_GNU_BINCL)
7095 unit = push_new_compile_unit (unit, c);
7096 else if (c->die_tag == DW_TAG_GNU_EINCL)
7097 unit = pop_compile_unit (unit);
7098 else
7099 add_child_die (unit, c);
7100 c = next;
7101 if (c == die->die_child)
7102 break;
7104 } while (c != die->die_child);
7106 #if 0
7107 /* We can only use this in debugging, since the frontend doesn't check
7108 to make sure that we leave every include file we enter. */
7109 gcc_assert (!unit);
7110 #endif
7112 assign_symbol_names (die);
7113 cu_hash_type cu_hash_table (10);
7114 for (node = limbo_die_list, pnode = &limbo_die_list;
7115 node;
7116 node = node->next)
7118 int is_dupl;
7120 compute_section_prefix (node->die);
7121 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7122 &comdat_symbol_number);
7123 assign_symbol_names (node->die);
7124 if (is_dupl)
7125 *pnode = node->next;
7126 else
7128 pnode = &node->next;
7129 record_comdat_symbol_number (node->die, &cu_hash_table,
7130 comdat_symbol_number);
7135 /* Return non-zero if this DIE is a declaration. */
7137 static int
7138 is_declaration_die (dw_die_ref die)
7140 dw_attr_node *a;
7141 unsigned ix;
7143 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7144 if (a->dw_attr == DW_AT_declaration)
7145 return 1;
7147 return 0;
7150 /* Return non-zero if this DIE is nested inside a subprogram. */
7152 static int
7153 is_nested_in_subprogram (dw_die_ref die)
7155 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7157 if (decl == NULL)
7158 decl = die;
7159 return local_scope_p (decl);
7162 /* Return non-zero if this DIE contains a defining declaration of a
7163 subprogram. */
7165 static int
7166 contains_subprogram_definition (dw_die_ref die)
7168 dw_die_ref c;
7170 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7171 return 1;
7172 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7173 return 0;
7176 /* Return non-zero if this is a type DIE that should be moved to a
7177 COMDAT .debug_types section. */
7179 static int
7180 should_move_die_to_comdat (dw_die_ref die)
7182 switch (die->die_tag)
7184 case DW_TAG_class_type:
7185 case DW_TAG_structure_type:
7186 case DW_TAG_enumeration_type:
7187 case DW_TAG_union_type:
7188 /* Don't move declarations, inlined instances, types nested in a
7189 subprogram, or types that contain subprogram definitions. */
7190 if (is_declaration_die (die)
7191 || get_AT (die, DW_AT_abstract_origin)
7192 || is_nested_in_subprogram (die)
7193 || contains_subprogram_definition (die))
7194 return 0;
7195 return 1;
7196 case DW_TAG_array_type:
7197 case DW_TAG_interface_type:
7198 case DW_TAG_pointer_type:
7199 case DW_TAG_reference_type:
7200 case DW_TAG_rvalue_reference_type:
7201 case DW_TAG_string_type:
7202 case DW_TAG_subroutine_type:
7203 case DW_TAG_ptr_to_member_type:
7204 case DW_TAG_set_type:
7205 case DW_TAG_subrange_type:
7206 case DW_TAG_base_type:
7207 case DW_TAG_const_type:
7208 case DW_TAG_file_type:
7209 case DW_TAG_packed_type:
7210 case DW_TAG_volatile_type:
7211 case DW_TAG_typedef:
7212 default:
7213 return 0;
7217 /* Make a clone of DIE. */
7219 static dw_die_ref
7220 clone_die (dw_die_ref die)
7222 dw_die_ref clone;
7223 dw_attr_node *a;
7224 unsigned ix;
7226 clone = ggc_cleared_alloc<die_node> ();
7227 clone->die_tag = die->die_tag;
7229 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7230 add_dwarf_attr (clone, a);
7232 return clone;
7235 /* Make a clone of the tree rooted at DIE. */
7237 static dw_die_ref
7238 clone_tree (dw_die_ref die)
7240 dw_die_ref c;
7241 dw_die_ref clone = clone_die (die);
7243 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7245 return clone;
7248 /* Make a clone of DIE as a declaration. */
7250 static dw_die_ref
7251 clone_as_declaration (dw_die_ref die)
7253 dw_die_ref clone;
7254 dw_die_ref decl;
7255 dw_attr_node *a;
7256 unsigned ix;
7258 /* If the DIE is already a declaration, just clone it. */
7259 if (is_declaration_die (die))
7260 return clone_die (die);
7262 /* If the DIE is a specification, just clone its declaration DIE. */
7263 decl = get_AT_ref (die, DW_AT_specification);
7264 if (decl != NULL)
7266 clone = clone_die (decl);
7267 if (die->comdat_type_p)
7268 add_AT_die_ref (clone, DW_AT_signature, die);
7269 return clone;
7272 clone = ggc_cleared_alloc<die_node> ();
7273 clone->die_tag = die->die_tag;
7275 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7277 /* We don't want to copy over all attributes.
7278 For example we don't want DW_AT_byte_size because otherwise we will no
7279 longer have a declaration and GDB will treat it as a definition. */
7281 switch (a->dw_attr)
7283 case DW_AT_abstract_origin:
7284 case DW_AT_artificial:
7285 case DW_AT_containing_type:
7286 case DW_AT_external:
7287 case DW_AT_name:
7288 case DW_AT_type:
7289 case DW_AT_virtuality:
7290 case DW_AT_linkage_name:
7291 case DW_AT_MIPS_linkage_name:
7292 add_dwarf_attr (clone, a);
7293 break;
7294 case DW_AT_byte_size:
7295 default:
7296 break;
7300 if (die->comdat_type_p)
7301 add_AT_die_ref (clone, DW_AT_signature, die);
7303 add_AT_flag (clone, DW_AT_declaration, 1);
7304 return clone;
7308 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7310 struct decl_table_entry
7312 dw_die_ref orig;
7313 dw_die_ref copy;
7316 /* Helpers to manipulate hash table of copied declarations. */
7318 /* Hashtable helpers. */
7320 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7322 typedef die_struct *compare_type;
7323 static inline hashval_t hash (const decl_table_entry *);
7324 static inline bool equal (const decl_table_entry *, const die_struct *);
7327 inline hashval_t
7328 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7330 return htab_hash_pointer (entry->orig);
7333 inline bool
7334 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7335 const die_struct *entry2)
7337 return entry1->orig == entry2;
7340 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7342 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7343 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7344 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7345 to check if the ancestor has already been copied into UNIT. */
7347 static dw_die_ref
7348 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7349 decl_hash_type *decl_table)
7351 dw_die_ref parent = die->die_parent;
7352 dw_die_ref new_parent = unit;
7353 dw_die_ref copy;
7354 decl_table_entry **slot = NULL;
7355 struct decl_table_entry *entry = NULL;
7357 if (decl_table)
7359 /* Check if the entry has already been copied to UNIT. */
7360 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7361 INSERT);
7362 if (*slot != HTAB_EMPTY_ENTRY)
7364 entry = *slot;
7365 return entry->copy;
7368 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7369 entry = XCNEW (struct decl_table_entry);
7370 entry->orig = die;
7371 entry->copy = NULL;
7372 *slot = entry;
7375 if (parent != NULL)
7377 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7378 if (spec != NULL)
7379 parent = spec;
7380 if (!is_unit_die (parent))
7381 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7384 copy = clone_as_declaration (die);
7385 add_child_die (new_parent, copy);
7387 if (decl_table)
7389 /* Record the pointer to the copy. */
7390 entry->copy = copy;
7393 return copy;
7395 /* Copy the declaration context to the new type unit DIE. This includes
7396 any surrounding namespace or type declarations. If the DIE has an
7397 AT_specification attribute, it also includes attributes and children
7398 attached to the specification, and returns a pointer to the original
7399 parent of the declaration DIE. Returns NULL otherwise. */
7401 static dw_die_ref
7402 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7404 dw_die_ref decl;
7405 dw_die_ref new_decl;
7406 dw_die_ref orig_parent = NULL;
7408 decl = get_AT_ref (die, DW_AT_specification);
7409 if (decl == NULL)
7410 decl = die;
7411 else
7413 unsigned ix;
7414 dw_die_ref c;
7415 dw_attr_node *a;
7417 /* The original DIE will be changed to a declaration, and must
7418 be moved to be a child of the original declaration DIE. */
7419 orig_parent = decl->die_parent;
7421 /* Copy the type node pointer from the new DIE to the original
7422 declaration DIE so we can forward references later. */
7423 decl->comdat_type_p = true;
7424 decl->die_id.die_type_node = die->die_id.die_type_node;
7426 remove_AT (die, DW_AT_specification);
7428 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7430 if (a->dw_attr != DW_AT_name
7431 && a->dw_attr != DW_AT_declaration
7432 && a->dw_attr != DW_AT_external)
7433 add_dwarf_attr (die, a);
7436 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7439 if (decl->die_parent != NULL
7440 && !is_unit_die (decl->die_parent))
7442 new_decl = copy_ancestor_tree (unit, decl, NULL);
7443 if (new_decl != NULL)
7445 remove_AT (new_decl, DW_AT_signature);
7446 add_AT_specification (die, new_decl);
7450 return orig_parent;
7453 /* Generate the skeleton ancestor tree for the given NODE, then clone
7454 the DIE and add the clone into the tree. */
7456 static void
7457 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7459 if (node->new_die != NULL)
7460 return;
7462 node->new_die = clone_as_declaration (node->old_die);
7464 if (node->parent != NULL)
7466 generate_skeleton_ancestor_tree (node->parent);
7467 add_child_die (node->parent->new_die, node->new_die);
7471 /* Generate a skeleton tree of DIEs containing any declarations that are
7472 found in the original tree. We traverse the tree looking for declaration
7473 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7475 static void
7476 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7478 skeleton_chain_node node;
7479 dw_die_ref c;
7480 dw_die_ref first;
7481 dw_die_ref prev = NULL;
7482 dw_die_ref next = NULL;
7484 node.parent = parent;
7486 first = c = parent->old_die->die_child;
7487 if (c)
7488 next = c->die_sib;
7489 if (c) do {
7490 if (prev == NULL || prev->die_sib == c)
7491 prev = c;
7492 c = next;
7493 next = (c == first ? NULL : c->die_sib);
7494 node.old_die = c;
7495 node.new_die = NULL;
7496 if (is_declaration_die (c))
7498 if (is_template_instantiation (c))
7500 /* Instantiated templates do not need to be cloned into the
7501 type unit. Just move the DIE and its children back to
7502 the skeleton tree (in the main CU). */
7503 remove_child_with_prev (c, prev);
7504 add_child_die (parent->new_die, c);
7505 c = prev;
7507 else
7509 /* Clone the existing DIE, move the original to the skeleton
7510 tree (which is in the main CU), and put the clone, with
7511 all the original's children, where the original came from
7512 (which is about to be moved to the type unit). */
7513 dw_die_ref clone = clone_die (c);
7514 move_all_children (c, clone);
7516 /* If the original has a DW_AT_object_pointer attribute,
7517 it would now point to a child DIE just moved to the
7518 cloned tree, so we need to remove that attribute from
7519 the original. */
7520 remove_AT (c, DW_AT_object_pointer);
7522 replace_child (c, clone, prev);
7523 generate_skeleton_ancestor_tree (parent);
7524 add_child_die (parent->new_die, c);
7525 node.new_die = c;
7526 c = clone;
7529 generate_skeleton_bottom_up (&node);
7530 } while (next != NULL);
7533 /* Wrapper function for generate_skeleton_bottom_up. */
7535 static dw_die_ref
7536 generate_skeleton (dw_die_ref die)
7538 skeleton_chain_node node;
7540 node.old_die = die;
7541 node.new_die = NULL;
7542 node.parent = NULL;
7544 /* If this type definition is nested inside another type,
7545 and is not an instantiation of a template, always leave
7546 at least a declaration in its place. */
7547 if (die->die_parent != NULL
7548 && is_type_die (die->die_parent)
7549 && !is_template_instantiation (die))
7550 node.new_die = clone_as_declaration (die);
7552 generate_skeleton_bottom_up (&node);
7553 return node.new_die;
7556 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7557 declaration. The original DIE is moved to a new compile unit so that
7558 existing references to it follow it to the new location. If any of the
7559 original DIE's descendants is a declaration, we need to replace the
7560 original DIE with a skeleton tree and move the declarations back into the
7561 skeleton tree. */
7563 static dw_die_ref
7564 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7565 dw_die_ref prev)
7567 dw_die_ref skeleton, orig_parent;
7569 /* Copy the declaration context to the type unit DIE. If the returned
7570 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7571 that DIE. */
7572 orig_parent = copy_declaration_context (unit, child);
7574 skeleton = generate_skeleton (child);
7575 if (skeleton == NULL)
7576 remove_child_with_prev (child, prev);
7577 else
7579 skeleton->comdat_type_p = true;
7580 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7582 /* If the original DIE was a specification, we need to put
7583 the skeleton under the parent DIE of the declaration.
7584 This leaves the original declaration in the tree, but
7585 it will be pruned later since there are no longer any
7586 references to it. */
7587 if (orig_parent != NULL)
7589 remove_child_with_prev (child, prev);
7590 add_child_die (orig_parent, skeleton);
7592 else
7593 replace_child (child, skeleton, prev);
7596 return skeleton;
7599 /* Traverse the DIE and set up additional .debug_types sections for each
7600 type worthy of being placed in a COMDAT section. */
7602 static void
7603 break_out_comdat_types (dw_die_ref die)
7605 dw_die_ref c;
7606 dw_die_ref first;
7607 dw_die_ref prev = NULL;
7608 dw_die_ref next = NULL;
7609 dw_die_ref unit = NULL;
7611 first = c = die->die_child;
7612 if (c)
7613 next = c->die_sib;
7614 if (c) do {
7615 if (prev == NULL || prev->die_sib == c)
7616 prev = c;
7617 c = next;
7618 next = (c == first ? NULL : c->die_sib);
7619 if (should_move_die_to_comdat (c))
7621 dw_die_ref replacement;
7622 comdat_type_node *type_node;
7624 /* Break out nested types into their own type units. */
7625 break_out_comdat_types (c);
7627 /* Create a new type unit DIE as the root for the new tree, and
7628 add it to the list of comdat types. */
7629 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7630 add_AT_unsigned (unit, DW_AT_language,
7631 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7632 type_node = ggc_cleared_alloc<comdat_type_node> ();
7633 type_node->root_die = unit;
7634 type_node->next = comdat_type_list;
7635 comdat_type_list = type_node;
7637 /* Generate the type signature. */
7638 generate_type_signature (c, type_node);
7640 /* Copy the declaration context, attributes, and children of the
7641 declaration into the new type unit DIE, then remove this DIE
7642 from the main CU (or replace it with a skeleton if necessary). */
7643 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7644 type_node->skeleton_die = replacement;
7646 /* Add the DIE to the new compunit. */
7647 add_child_die (unit, c);
7649 if (replacement != NULL)
7650 c = replacement;
7652 else if (c->die_tag == DW_TAG_namespace
7653 || c->die_tag == DW_TAG_class_type
7654 || c->die_tag == DW_TAG_structure_type
7655 || c->die_tag == DW_TAG_union_type)
7657 /* Look for nested types that can be broken out. */
7658 break_out_comdat_types (c);
7660 } while (next != NULL);
7663 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7664 Enter all the cloned children into the hash table decl_table. */
7666 static dw_die_ref
7667 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7669 dw_die_ref c;
7670 dw_die_ref clone;
7671 struct decl_table_entry *entry;
7672 decl_table_entry **slot;
7674 if (die->die_tag == DW_TAG_subprogram)
7675 clone = clone_as_declaration (die);
7676 else
7677 clone = clone_die (die);
7679 slot = decl_table->find_slot_with_hash (die,
7680 htab_hash_pointer (die), INSERT);
7682 /* Assert that DIE isn't in the hash table yet. If it would be there
7683 before, the ancestors would be necessarily there as well, therefore
7684 clone_tree_partial wouldn't be called. */
7685 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7687 entry = XCNEW (struct decl_table_entry);
7688 entry->orig = die;
7689 entry->copy = clone;
7690 *slot = entry;
7692 if (die->die_tag != DW_TAG_subprogram)
7693 FOR_EACH_CHILD (die, c,
7694 add_child_die (clone, clone_tree_partial (c, decl_table)));
7696 return clone;
7699 /* Walk the DIE and its children, looking for references to incomplete
7700 or trivial types that are unmarked (i.e., that are not in the current
7701 type_unit). */
7703 static void
7704 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7706 dw_die_ref c;
7707 dw_attr_node *a;
7708 unsigned ix;
7710 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7712 if (AT_class (a) == dw_val_class_die_ref)
7714 dw_die_ref targ = AT_ref (a);
7715 decl_table_entry **slot;
7716 struct decl_table_entry *entry;
7718 if (targ->die_mark != 0 || targ->comdat_type_p)
7719 continue;
7721 slot = decl_table->find_slot_with_hash (targ,
7722 htab_hash_pointer (targ),
7723 INSERT);
7725 if (*slot != HTAB_EMPTY_ENTRY)
7727 /* TARG has already been copied, so we just need to
7728 modify the reference to point to the copy. */
7729 entry = *slot;
7730 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7732 else
7734 dw_die_ref parent = unit;
7735 dw_die_ref copy = clone_die (targ);
7737 /* Record in DECL_TABLE that TARG has been copied.
7738 Need to do this now, before the recursive call,
7739 because DECL_TABLE may be expanded and SLOT
7740 would no longer be a valid pointer. */
7741 entry = XCNEW (struct decl_table_entry);
7742 entry->orig = targ;
7743 entry->copy = copy;
7744 *slot = entry;
7746 /* If TARG is not a declaration DIE, we need to copy its
7747 children. */
7748 if (!is_declaration_die (targ))
7750 FOR_EACH_CHILD (
7751 targ, c,
7752 add_child_die (copy,
7753 clone_tree_partial (c, decl_table)));
7756 /* Make sure the cloned tree is marked as part of the
7757 type unit. */
7758 mark_dies (copy);
7760 /* If TARG has surrounding context, copy its ancestor tree
7761 into the new type unit. */
7762 if (targ->die_parent != NULL
7763 && !is_unit_die (targ->die_parent))
7764 parent = copy_ancestor_tree (unit, targ->die_parent,
7765 decl_table);
7767 add_child_die (parent, copy);
7768 a->dw_attr_val.v.val_die_ref.die = copy;
7770 /* Make sure the newly-copied DIE is walked. If it was
7771 installed in a previously-added context, it won't
7772 get visited otherwise. */
7773 if (parent != unit)
7775 /* Find the highest point of the newly-added tree,
7776 mark each node along the way, and walk from there. */
7777 parent->die_mark = 1;
7778 while (parent->die_parent
7779 && parent->die_parent->die_mark == 0)
7781 parent = parent->die_parent;
7782 parent->die_mark = 1;
7784 copy_decls_walk (unit, parent, decl_table);
7790 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7793 /* Copy declarations for "unworthy" types into the new comdat section.
7794 Incomplete types, modified types, and certain other types aren't broken
7795 out into comdat sections of their own, so they don't have a signature,
7796 and we need to copy the declaration into the same section so that we
7797 don't have an external reference. */
7799 static void
7800 copy_decls_for_unworthy_types (dw_die_ref unit)
7802 mark_dies (unit);
7803 decl_hash_type decl_table (10);
7804 copy_decls_walk (unit, unit, &decl_table);
7805 unmark_dies (unit);
7808 /* Traverse the DIE and add a sibling attribute if it may have the
7809 effect of speeding up access to siblings. To save some space,
7810 avoid generating sibling attributes for DIE's without children. */
7812 static void
7813 add_sibling_attributes (dw_die_ref die)
7815 dw_die_ref c;
7817 if (! die->die_child)
7818 return;
7820 if (die->die_parent && die != die->die_parent->die_child)
7821 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7823 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7826 /* Output all location lists for the DIE and its children. */
7828 static void
7829 output_location_lists (dw_die_ref die)
7831 dw_die_ref c;
7832 dw_attr_node *a;
7833 unsigned ix;
7835 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7836 if (AT_class (a) == dw_val_class_loc_list)
7837 output_loc_list (AT_loc_list (a));
7839 FOR_EACH_CHILD (die, c, output_location_lists (c));
7842 /* We want to limit the number of external references, because they are
7843 larger than local references: a relocation takes multiple words, and
7844 even a sig8 reference is always eight bytes, whereas a local reference
7845 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7846 So if we encounter multiple external references to the same type DIE, we
7847 make a local typedef stub for it and redirect all references there.
7849 This is the element of the hash table for keeping track of these
7850 references. */
7852 struct external_ref
7854 dw_die_ref type;
7855 dw_die_ref stub;
7856 unsigned n_refs;
7859 /* Hashtable helpers. */
7861 struct external_ref_hasher : free_ptr_hash <external_ref>
7863 static inline hashval_t hash (const external_ref *);
7864 static inline bool equal (const external_ref *, const external_ref *);
7867 inline hashval_t
7868 external_ref_hasher::hash (const external_ref *r)
7870 dw_die_ref die = r->type;
7871 hashval_t h = 0;
7873 /* We can't use the address of the DIE for hashing, because
7874 that will make the order of the stub DIEs non-deterministic. */
7875 if (! die->comdat_type_p)
7876 /* We have a symbol; use it to compute a hash. */
7877 h = htab_hash_string (die->die_id.die_symbol);
7878 else
7880 /* We have a type signature; use a subset of the bits as the hash.
7881 The 8-byte signature is at least as large as hashval_t. */
7882 comdat_type_node *type_node = die->die_id.die_type_node;
7883 memcpy (&h, type_node->signature, sizeof (h));
7885 return h;
7888 inline bool
7889 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
7891 return r1->type == r2->type;
7894 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7896 /* Return a pointer to the external_ref for references to DIE. */
7898 static struct external_ref *
7899 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7901 struct external_ref ref, *ref_p;
7902 external_ref **slot;
7904 ref.type = die;
7905 slot = map->find_slot (&ref, INSERT);
7906 if (*slot != HTAB_EMPTY_ENTRY)
7907 return *slot;
7909 ref_p = XCNEW (struct external_ref);
7910 ref_p->type = die;
7911 *slot = ref_p;
7912 return ref_p;
7915 /* Subroutine of optimize_external_refs, below.
7917 If we see a type skeleton, record it as our stub. If we see external
7918 references, remember how many we've seen. */
7920 static void
7921 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7923 dw_die_ref c;
7924 dw_attr_node *a;
7925 unsigned ix;
7926 struct external_ref *ref_p;
7928 if (is_type_die (die)
7929 && (c = get_AT_ref (die, DW_AT_signature)))
7931 /* This is a local skeleton; use it for local references. */
7932 ref_p = lookup_external_ref (map, c);
7933 ref_p->stub = die;
7936 /* Scan the DIE references, and remember any that refer to DIEs from
7937 other CUs (i.e. those which are not marked). */
7938 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7939 if (AT_class (a) == dw_val_class_die_ref
7940 && (c = AT_ref (a))->die_mark == 0
7941 && is_type_die (c))
7943 ref_p = lookup_external_ref (map, c);
7944 ref_p->n_refs++;
7947 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7950 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7951 points to an external_ref, DATA is the CU we're processing. If we don't
7952 already have a local stub, and we have multiple refs, build a stub. */
7955 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7957 struct external_ref *ref_p = *slot;
7959 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7961 /* We have multiple references to this type, so build a small stub.
7962 Both of these forms are a bit dodgy from the perspective of the
7963 DWARF standard, since technically they should have names. */
7964 dw_die_ref cu = data;
7965 dw_die_ref type = ref_p->type;
7966 dw_die_ref stub = NULL;
7968 if (type->comdat_type_p)
7970 /* If we refer to this type via sig8, use AT_signature. */
7971 stub = new_die (type->die_tag, cu, NULL_TREE);
7972 add_AT_die_ref (stub, DW_AT_signature, type);
7974 else
7976 /* Otherwise, use a typedef with no name. */
7977 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7978 add_AT_die_ref (stub, DW_AT_type, type);
7981 stub->die_mark++;
7982 ref_p->stub = stub;
7984 return 1;
7987 /* DIE is a unit; look through all the DIE references to see if there are
7988 any external references to types, and if so, create local stubs for
7989 them which will be applied in build_abbrev_table. This is useful because
7990 references to local DIEs are smaller. */
7992 static external_ref_hash_type *
7993 optimize_external_refs (dw_die_ref die)
7995 external_ref_hash_type *map = new external_ref_hash_type (10);
7996 optimize_external_refs_1 (die, map);
7997 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7998 return map;
8001 /* The format of each DIE (and its attribute value pairs) is encoded in an
8002 abbreviation table. This routine builds the abbreviation table and assigns
8003 a unique abbreviation id for each abbreviation entry. The children of each
8004 die are visited recursively. */
8006 static void
8007 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8009 unsigned long abbrev_id;
8010 unsigned int n_alloc;
8011 dw_die_ref c;
8012 dw_attr_node *a;
8013 unsigned ix;
8015 /* Scan the DIE references, and replace any that refer to
8016 DIEs from other CUs (i.e. those which are not marked) with
8017 the local stubs we built in optimize_external_refs. */
8018 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8019 if (AT_class (a) == dw_val_class_die_ref
8020 && (c = AT_ref (a))->die_mark == 0)
8022 struct external_ref *ref_p;
8023 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8025 ref_p = lookup_external_ref (extern_map, c);
8026 if (ref_p->stub && ref_p->stub != die)
8027 change_AT_die_ref (a, ref_p->stub);
8028 else
8029 /* We aren't changing this reference, so mark it external. */
8030 set_AT_ref_external (a, 1);
8033 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8035 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8036 dw_attr_node *die_a, *abbrev_a;
8037 unsigned ix;
8038 bool ok = true;
8040 if (abbrev->die_tag != die->die_tag)
8041 continue;
8042 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8043 continue;
8045 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8046 continue;
8048 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8050 abbrev_a = &(*abbrev->die_attr)[ix];
8051 if ((abbrev_a->dw_attr != die_a->dw_attr)
8052 || (value_format (abbrev_a) != value_format (die_a)))
8054 ok = false;
8055 break;
8058 if (ok)
8059 break;
8062 if (abbrev_id >= abbrev_die_table_in_use)
8064 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8066 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8067 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8068 n_alloc);
8070 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8071 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8072 abbrev_die_table_allocated = n_alloc;
8075 ++abbrev_die_table_in_use;
8076 abbrev_die_table[abbrev_id] = die;
8079 die->die_abbrev = abbrev_id;
8080 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8083 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8085 static int
8086 constant_size (unsigned HOST_WIDE_INT value)
8088 int log;
8090 if (value == 0)
8091 log = 0;
8092 else
8093 log = floor_log2 (value);
8095 log = log / 8;
8096 log = 1 << (floor_log2 (log) + 1);
8098 return log;
8101 /* Return the size of a DIE as it is represented in the
8102 .debug_info section. */
8104 static unsigned long
8105 size_of_die (dw_die_ref die)
8107 unsigned long size = 0;
8108 dw_attr_node *a;
8109 unsigned ix;
8110 enum dwarf_form form;
8112 size += size_of_uleb128 (die->die_abbrev);
8113 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8115 switch (AT_class (a))
8117 case dw_val_class_addr:
8118 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8120 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8121 size += size_of_uleb128 (AT_index (a));
8123 else
8124 size += DWARF2_ADDR_SIZE;
8125 break;
8126 case dw_val_class_offset:
8127 size += DWARF_OFFSET_SIZE;
8128 break;
8129 case dw_val_class_loc:
8131 unsigned long lsize = size_of_locs (AT_loc (a));
8133 /* Block length. */
8134 if (dwarf_version >= 4)
8135 size += size_of_uleb128 (lsize);
8136 else
8137 size += constant_size (lsize);
8138 size += lsize;
8140 break;
8141 case dw_val_class_loc_list:
8142 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8144 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8145 size += size_of_uleb128 (AT_index (a));
8147 else
8148 size += DWARF_OFFSET_SIZE;
8149 break;
8150 case dw_val_class_range_list:
8151 size += DWARF_OFFSET_SIZE;
8152 break;
8153 case dw_val_class_const:
8154 size += size_of_sleb128 (AT_int (a));
8155 break;
8156 case dw_val_class_unsigned_const:
8158 int csize = constant_size (AT_unsigned (a));
8159 if (dwarf_version == 3
8160 && a->dw_attr == DW_AT_data_member_location
8161 && csize >= 4)
8162 size += size_of_uleb128 (AT_unsigned (a));
8163 else
8164 size += csize;
8166 break;
8167 case dw_val_class_const_double:
8168 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8169 if (HOST_BITS_PER_WIDE_INT >= 64)
8170 size++; /* block */
8171 break;
8172 case dw_val_class_wide_int:
8173 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8174 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8175 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8176 > 64)
8177 size++; /* block */
8178 break;
8179 case dw_val_class_vec:
8180 size += constant_size (a->dw_attr_val.v.val_vec.length
8181 * a->dw_attr_val.v.val_vec.elt_size)
8182 + a->dw_attr_val.v.val_vec.length
8183 * a->dw_attr_val.v.val_vec.elt_size; /* block */
8184 break;
8185 case dw_val_class_flag:
8186 if (dwarf_version >= 4)
8187 /* Currently all add_AT_flag calls pass in 1 as last argument,
8188 so DW_FORM_flag_present can be used. If that ever changes,
8189 we'll need to use DW_FORM_flag and have some optimization
8190 in build_abbrev_table that will change those to
8191 DW_FORM_flag_present if it is set to 1 in all DIEs using
8192 the same abbrev entry. */
8193 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8194 else
8195 size += 1;
8196 break;
8197 case dw_val_class_die_ref:
8198 if (AT_ref_external (a))
8200 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8201 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
8202 is sized by target address length, whereas in DWARF3
8203 it's always sized as an offset. */
8204 if (use_debug_types)
8205 size += DWARF_TYPE_SIGNATURE_SIZE;
8206 else if (dwarf_version == 2)
8207 size += DWARF2_ADDR_SIZE;
8208 else
8209 size += DWARF_OFFSET_SIZE;
8211 else
8212 size += DWARF_OFFSET_SIZE;
8213 break;
8214 case dw_val_class_fde_ref:
8215 size += DWARF_OFFSET_SIZE;
8216 break;
8217 case dw_val_class_lbl_id:
8218 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8220 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8221 size += size_of_uleb128 (AT_index (a));
8223 else
8224 size += DWARF2_ADDR_SIZE;
8225 break;
8226 case dw_val_class_lineptr:
8227 case dw_val_class_macptr:
8228 size += DWARF_OFFSET_SIZE;
8229 break;
8230 case dw_val_class_str:
8231 form = AT_string_form (a);
8232 if (form == DW_FORM_strp)
8233 size += DWARF_OFFSET_SIZE;
8234 else if (form == DW_FORM_GNU_str_index)
8235 size += size_of_uleb128 (AT_index (a));
8236 else
8237 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8238 break;
8239 case dw_val_class_file:
8240 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8241 break;
8242 case dw_val_class_data8:
8243 size += 8;
8244 break;
8245 case dw_val_class_vms_delta:
8246 size += DWARF_OFFSET_SIZE;
8247 break;
8248 case dw_val_class_high_pc:
8249 size += DWARF2_ADDR_SIZE;
8250 break;
8251 default:
8252 gcc_unreachable ();
8256 return size;
8259 /* Size the debugging information associated with a given DIE. Visits the
8260 DIE's children recursively. Updates the global variable next_die_offset, on
8261 each time through. Uses the current value of next_die_offset to update the
8262 die_offset field in each DIE. */
8264 static void
8265 calc_die_sizes (dw_die_ref die)
8267 dw_die_ref c;
8269 gcc_assert (die->die_offset == 0
8270 || (unsigned long int) die->die_offset == next_die_offset);
8271 die->die_offset = next_die_offset;
8272 next_die_offset += size_of_die (die);
8274 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8276 if (die->die_child != NULL)
8277 /* Count the null byte used to terminate sibling lists. */
8278 next_die_offset += 1;
8281 /* Size just the base type children at the start of the CU.
8282 This is needed because build_abbrev needs to size locs
8283 and sizing of type based stack ops needs to know die_offset
8284 values for the base types. */
8286 static void
8287 calc_base_type_die_sizes (void)
8289 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8290 unsigned int i;
8291 dw_die_ref base_type;
8292 #if ENABLE_ASSERT_CHECKING
8293 dw_die_ref prev = comp_unit_die ()->die_child;
8294 #endif
8296 die_offset += size_of_die (comp_unit_die ());
8297 for (i = 0; base_types.iterate (i, &base_type); i++)
8299 #if ENABLE_ASSERT_CHECKING
8300 gcc_assert (base_type->die_offset == 0
8301 && prev->die_sib == base_type
8302 && base_type->die_child == NULL
8303 && base_type->die_abbrev);
8304 prev = base_type;
8305 #endif
8306 base_type->die_offset = die_offset;
8307 die_offset += size_of_die (base_type);
8311 /* Set the marks for a die and its children. We do this so
8312 that we know whether or not a reference needs to use FORM_ref_addr; only
8313 DIEs in the same CU will be marked. We used to clear out the offset
8314 and use that as the flag, but ran into ordering problems. */
8316 static void
8317 mark_dies (dw_die_ref die)
8319 dw_die_ref c;
8321 gcc_assert (!die->die_mark);
8323 die->die_mark = 1;
8324 FOR_EACH_CHILD (die, c, mark_dies (c));
8327 /* Clear the marks for a die and its children. */
8329 static void
8330 unmark_dies (dw_die_ref die)
8332 dw_die_ref c;
8334 if (! use_debug_types)
8335 gcc_assert (die->die_mark);
8337 die->die_mark = 0;
8338 FOR_EACH_CHILD (die, c, unmark_dies (c));
8341 /* Clear the marks for a die, its children and referred dies. */
8343 static void
8344 unmark_all_dies (dw_die_ref die)
8346 dw_die_ref c;
8347 dw_attr_node *a;
8348 unsigned ix;
8350 if (!die->die_mark)
8351 return;
8352 die->die_mark = 0;
8354 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8356 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8357 if (AT_class (a) == dw_val_class_die_ref)
8358 unmark_all_dies (AT_ref (a));
8361 /* Calculate if the entry should appear in the final output file. It may be
8362 from a pruned a type. */
8364 static bool
8365 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8367 /* By limiting gnu pubnames to definitions only, gold can generate a
8368 gdb index without entries for declarations, which don't include
8369 enough information to be useful. */
8370 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8371 return false;
8373 if (table == pubname_table)
8375 /* Enumerator names are part of the pubname table, but the
8376 parent DW_TAG_enumeration_type die may have been pruned.
8377 Don't output them if that is the case. */
8378 if (p->die->die_tag == DW_TAG_enumerator &&
8379 (p->die->die_parent == NULL
8380 || !p->die->die_parent->die_perennial_p))
8381 return false;
8383 /* Everything else in the pubname table is included. */
8384 return true;
8387 /* The pubtypes table shouldn't include types that have been
8388 pruned. */
8389 return (p->die->die_offset != 0
8390 || !flag_eliminate_unused_debug_types);
8393 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8394 generated for the compilation unit. */
8396 static unsigned long
8397 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8399 unsigned long size;
8400 unsigned i;
8401 pubname_entry *p;
8402 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8404 size = DWARF_PUBNAMES_HEADER_SIZE;
8405 FOR_EACH_VEC_ELT (*names, i, p)
8406 if (include_pubname_in_output (names, p))
8407 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8409 size += DWARF_OFFSET_SIZE;
8410 return size;
8413 /* Return the size of the information in the .debug_aranges section. */
8415 static unsigned long
8416 size_of_aranges (void)
8418 unsigned long size;
8420 size = DWARF_ARANGES_HEADER_SIZE;
8422 /* Count the address/length pair for this compilation unit. */
8423 if (text_section_used)
8424 size += 2 * DWARF2_ADDR_SIZE;
8425 if (cold_text_section_used)
8426 size += 2 * DWARF2_ADDR_SIZE;
8427 if (have_multiple_function_sections)
8429 unsigned fde_idx;
8430 dw_fde_ref fde;
8432 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8434 if (DECL_IGNORED_P (fde->decl))
8435 continue;
8436 if (!fde->in_std_section)
8437 size += 2 * DWARF2_ADDR_SIZE;
8438 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8439 size += 2 * DWARF2_ADDR_SIZE;
8443 /* Count the two zero words used to terminated the address range table. */
8444 size += 2 * DWARF2_ADDR_SIZE;
8445 return size;
8448 /* Select the encoding of an attribute value. */
8450 static enum dwarf_form
8451 value_format (dw_attr_node *a)
8453 switch (AT_class (a))
8455 case dw_val_class_addr:
8456 /* Only very few attributes allow DW_FORM_addr. */
8457 switch (a->dw_attr)
8459 case DW_AT_low_pc:
8460 case DW_AT_high_pc:
8461 case DW_AT_entry_pc:
8462 case DW_AT_trampoline:
8463 return (AT_index (a) == NOT_INDEXED
8464 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8465 default:
8466 break;
8468 switch (DWARF2_ADDR_SIZE)
8470 case 1:
8471 return DW_FORM_data1;
8472 case 2:
8473 return DW_FORM_data2;
8474 case 4:
8475 return DW_FORM_data4;
8476 case 8:
8477 return DW_FORM_data8;
8478 default:
8479 gcc_unreachable ();
8481 case dw_val_class_range_list:
8482 case dw_val_class_loc_list:
8483 if (dwarf_version >= 4)
8484 return DW_FORM_sec_offset;
8485 /* FALLTHRU */
8486 case dw_val_class_vms_delta:
8487 case dw_val_class_offset:
8488 switch (DWARF_OFFSET_SIZE)
8490 case 4:
8491 return DW_FORM_data4;
8492 case 8:
8493 return DW_FORM_data8;
8494 default:
8495 gcc_unreachable ();
8497 case dw_val_class_loc:
8498 if (dwarf_version >= 4)
8499 return DW_FORM_exprloc;
8500 switch (constant_size (size_of_locs (AT_loc (a))))
8502 case 1:
8503 return DW_FORM_block1;
8504 case 2:
8505 return DW_FORM_block2;
8506 case 4:
8507 return DW_FORM_block4;
8508 default:
8509 gcc_unreachable ();
8511 case dw_val_class_const:
8512 return DW_FORM_sdata;
8513 case dw_val_class_unsigned_const:
8514 switch (constant_size (AT_unsigned (a)))
8516 case 1:
8517 return DW_FORM_data1;
8518 case 2:
8519 return DW_FORM_data2;
8520 case 4:
8521 /* In DWARF3 DW_AT_data_member_location with
8522 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8523 constant, so we need to use DW_FORM_udata if we need
8524 a large constant. */
8525 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8526 return DW_FORM_udata;
8527 return DW_FORM_data4;
8528 case 8:
8529 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8530 return DW_FORM_udata;
8531 return DW_FORM_data8;
8532 default:
8533 gcc_unreachable ();
8535 case dw_val_class_const_double:
8536 switch (HOST_BITS_PER_WIDE_INT)
8538 case 8:
8539 return DW_FORM_data2;
8540 case 16:
8541 return DW_FORM_data4;
8542 case 32:
8543 return DW_FORM_data8;
8544 case 64:
8545 default:
8546 return DW_FORM_block1;
8548 case dw_val_class_wide_int:
8549 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8551 case 8:
8552 return DW_FORM_data1;
8553 case 16:
8554 return DW_FORM_data2;
8555 case 32:
8556 return DW_FORM_data4;
8557 case 64:
8558 return DW_FORM_data8;
8559 default:
8560 return DW_FORM_block1;
8562 case dw_val_class_vec:
8563 switch (constant_size (a->dw_attr_val.v.val_vec.length
8564 * a->dw_attr_val.v.val_vec.elt_size))
8566 case 1:
8567 return DW_FORM_block1;
8568 case 2:
8569 return DW_FORM_block2;
8570 case 4:
8571 return DW_FORM_block4;
8572 default:
8573 gcc_unreachable ();
8575 case dw_val_class_flag:
8576 if (dwarf_version >= 4)
8578 /* Currently all add_AT_flag calls pass in 1 as last argument,
8579 so DW_FORM_flag_present can be used. If that ever changes,
8580 we'll need to use DW_FORM_flag and have some optimization
8581 in build_abbrev_table that will change those to
8582 DW_FORM_flag_present if it is set to 1 in all DIEs using
8583 the same abbrev entry. */
8584 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8585 return DW_FORM_flag_present;
8587 return DW_FORM_flag;
8588 case dw_val_class_die_ref:
8589 if (AT_ref_external (a))
8590 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8591 else
8592 return DW_FORM_ref;
8593 case dw_val_class_fde_ref:
8594 return DW_FORM_data;
8595 case dw_val_class_lbl_id:
8596 return (AT_index (a) == NOT_INDEXED
8597 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8598 case dw_val_class_lineptr:
8599 case dw_val_class_macptr:
8600 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8601 case dw_val_class_str:
8602 return AT_string_form (a);
8603 case dw_val_class_file:
8604 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8606 case 1:
8607 return DW_FORM_data1;
8608 case 2:
8609 return DW_FORM_data2;
8610 case 4:
8611 return DW_FORM_data4;
8612 default:
8613 gcc_unreachable ();
8616 case dw_val_class_data8:
8617 return DW_FORM_data8;
8619 case dw_val_class_high_pc:
8620 switch (DWARF2_ADDR_SIZE)
8622 case 1:
8623 return DW_FORM_data1;
8624 case 2:
8625 return DW_FORM_data2;
8626 case 4:
8627 return DW_FORM_data4;
8628 case 8:
8629 return DW_FORM_data8;
8630 default:
8631 gcc_unreachable ();
8634 default:
8635 gcc_unreachable ();
8639 /* Output the encoding of an attribute value. */
8641 static void
8642 output_value_format (dw_attr_node *a)
8644 enum dwarf_form form = value_format (a);
8646 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8649 /* Given a die and id, produce the appropriate abbreviations. */
8651 static void
8652 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8654 unsigned ix;
8655 dw_attr_node *a_attr;
8657 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8658 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8659 dwarf_tag_name (abbrev->die_tag));
8661 if (abbrev->die_child != NULL)
8662 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8663 else
8664 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8666 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8668 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8669 dwarf_attr_name (a_attr->dw_attr));
8670 output_value_format (a_attr);
8673 dw2_asm_output_data (1, 0, NULL);
8674 dw2_asm_output_data (1, 0, NULL);
8678 /* Output the .debug_abbrev section which defines the DIE abbreviation
8679 table. */
8681 static void
8682 output_abbrev_section (void)
8684 unsigned long abbrev_id;
8686 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8687 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8689 /* Terminate the table. */
8690 dw2_asm_output_data (1, 0, NULL);
8693 /* Output a symbol we can use to refer to this DIE from another CU. */
8695 static inline void
8696 output_die_symbol (dw_die_ref die)
8698 const char *sym = die->die_id.die_symbol;
8700 gcc_assert (!die->comdat_type_p);
8702 if (sym == 0)
8703 return;
8705 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8706 /* We make these global, not weak; if the target doesn't support
8707 .linkonce, it doesn't support combining the sections, so debugging
8708 will break. */
8709 targetm.asm_out.globalize_label (asm_out_file, sym);
8711 ASM_OUTPUT_LABEL (asm_out_file, sym);
8714 /* Return a new location list, given the begin and end range, and the
8715 expression. */
8717 static inline dw_loc_list_ref
8718 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8719 const char *section)
8721 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8723 retlist->begin = begin;
8724 retlist->begin_entry = NULL;
8725 retlist->end = end;
8726 retlist->expr = expr;
8727 retlist->section = section;
8729 return retlist;
8732 /* Generate a new internal symbol for this location list node, if it
8733 hasn't got one yet. */
8735 static inline void
8736 gen_llsym (dw_loc_list_ref list)
8738 gcc_assert (!list->ll_symbol);
8739 list->ll_symbol = gen_internal_sym ("LLST");
8742 /* Output the location list given to us. */
8744 static void
8745 output_loc_list (dw_loc_list_ref list_head)
8747 dw_loc_list_ref curr = list_head;
8749 if (list_head->emitted)
8750 return;
8751 list_head->emitted = true;
8753 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8755 /* Walk the location list, and output each range + expression. */
8756 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8758 unsigned long size;
8759 /* Don't output an entry that starts and ends at the same address. */
8760 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8761 continue;
8762 size = size_of_locs (curr->expr);
8763 /* If the expression is too large, drop it on the floor. We could
8764 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8765 in the expression, but >= 64KB expressions for a single value
8766 in a single range are unlikely very useful. */
8767 if (size > 0xffff)
8768 continue;
8769 if (dwarf_split_debug_info)
8771 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8772 "Location list start/length entry (%s)",
8773 list_head->ll_symbol);
8774 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8775 "Location list range start index (%s)",
8776 curr->begin);
8777 /* The length field is 4 bytes. If we ever need to support
8778 an 8-byte length, we can add a new DW_LLE code or fall back
8779 to DW_LLE_GNU_start_end_entry. */
8780 dw2_asm_output_delta (4, curr->end, curr->begin,
8781 "Location list range length (%s)",
8782 list_head->ll_symbol);
8784 else if (!have_multiple_function_sections)
8786 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8787 "Location list begin address (%s)",
8788 list_head->ll_symbol);
8789 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8790 "Location list end address (%s)",
8791 list_head->ll_symbol);
8793 else
8795 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8796 "Location list begin address (%s)",
8797 list_head->ll_symbol);
8798 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8799 "Location list end address (%s)",
8800 list_head->ll_symbol);
8803 /* Output the block length for this list of location operations. */
8804 gcc_assert (size <= 0xffff);
8805 dw2_asm_output_data (2, size, "%s", "Location expression size");
8807 output_loc_sequence (curr->expr, -1);
8810 if (dwarf_split_debug_info)
8811 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8812 "Location list terminator (%s)",
8813 list_head->ll_symbol);
8814 else
8816 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8817 "Location list terminator begin (%s)",
8818 list_head->ll_symbol);
8819 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8820 "Location list terminator end (%s)",
8821 list_head->ll_symbol);
8825 /* Output a range_list offset into the debug_range section. Emit a
8826 relocated reference if val_entry is NULL, otherwise, emit an
8827 indirect reference. */
8829 static void
8830 output_range_list_offset (dw_attr_node *a)
8832 const char *name = dwarf_attr_name (a->dw_attr);
8834 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8836 char *p = strchr (ranges_section_label, '\0');
8837 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8838 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8839 debug_ranges_section, "%s", name);
8840 *p = '\0';
8842 else
8843 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8844 "%s (offset from %s)", name, ranges_section_label);
8847 /* Output the offset into the debug_loc section. */
8849 static void
8850 output_loc_list_offset (dw_attr_node *a)
8852 char *sym = AT_loc_list (a)->ll_symbol;
8854 gcc_assert (sym);
8855 if (dwarf_split_debug_info)
8856 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8857 "%s", dwarf_attr_name (a->dw_attr));
8858 else
8859 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8860 "%s", dwarf_attr_name (a->dw_attr));
8863 /* Output an attribute's index or value appropriately. */
8865 static void
8866 output_attr_index_or_value (dw_attr_node *a)
8868 const char *name = dwarf_attr_name (a->dw_attr);
8870 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8872 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8873 return;
8875 switch (AT_class (a))
8877 case dw_val_class_addr:
8878 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8879 break;
8880 case dw_val_class_high_pc:
8881 case dw_val_class_lbl_id:
8882 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8883 break;
8884 case dw_val_class_loc_list:
8885 output_loc_list_offset (a);
8886 break;
8887 default:
8888 gcc_unreachable ();
8892 /* Output a type signature. */
8894 static inline void
8895 output_signature (const char *sig, const char *name)
8897 int i;
8899 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8900 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8903 /* Output the DIE and its attributes. Called recursively to generate
8904 the definitions of each child DIE. */
8906 static void
8907 output_die (dw_die_ref die)
8909 dw_attr_node *a;
8910 dw_die_ref c;
8911 unsigned long size;
8912 unsigned ix;
8914 /* If someone in another CU might refer to us, set up a symbol for
8915 them to point to. */
8916 if (! die->comdat_type_p && die->die_id.die_symbol)
8917 output_die_symbol (die);
8919 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8920 (unsigned long)die->die_offset,
8921 dwarf_tag_name (die->die_tag));
8923 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8925 const char *name = dwarf_attr_name (a->dw_attr);
8927 switch (AT_class (a))
8929 case dw_val_class_addr:
8930 output_attr_index_or_value (a);
8931 break;
8933 case dw_val_class_offset:
8934 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8935 "%s", name);
8936 break;
8938 case dw_val_class_range_list:
8939 output_range_list_offset (a);
8940 break;
8942 case dw_val_class_loc:
8943 size = size_of_locs (AT_loc (a));
8945 /* Output the block length for this list of location operations. */
8946 if (dwarf_version >= 4)
8947 dw2_asm_output_data_uleb128 (size, "%s", name);
8948 else
8949 dw2_asm_output_data (constant_size (size), size, "%s", name);
8951 output_loc_sequence (AT_loc (a), -1);
8952 break;
8954 case dw_val_class_const:
8955 /* ??? It would be slightly more efficient to use a scheme like is
8956 used for unsigned constants below, but gdb 4.x does not sign
8957 extend. Gdb 5.x does sign extend. */
8958 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8959 break;
8961 case dw_val_class_unsigned_const:
8963 int csize = constant_size (AT_unsigned (a));
8964 if (dwarf_version == 3
8965 && a->dw_attr == DW_AT_data_member_location
8966 && csize >= 4)
8967 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8968 else
8969 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8971 break;
8973 case dw_val_class_const_double:
8975 unsigned HOST_WIDE_INT first, second;
8977 if (HOST_BITS_PER_WIDE_INT >= 64)
8978 dw2_asm_output_data (1,
8979 HOST_BITS_PER_DOUBLE_INT
8980 / HOST_BITS_PER_CHAR,
8981 NULL);
8983 if (WORDS_BIG_ENDIAN)
8985 first = a->dw_attr_val.v.val_double.high;
8986 second = a->dw_attr_val.v.val_double.low;
8988 else
8990 first = a->dw_attr_val.v.val_double.low;
8991 second = a->dw_attr_val.v.val_double.high;
8994 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8995 first, "%s", name);
8996 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8997 second, NULL);
8999 break;
9001 case dw_val_class_wide_int:
9003 int i;
9004 int len = get_full_len (*a->dw_attr_val.v.val_wide);
9005 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
9006 if (len * HOST_BITS_PER_WIDE_INT > 64)
9007 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
9008 NULL);
9010 if (WORDS_BIG_ENDIAN)
9011 for (i = len - 1; i >= 0; --i)
9013 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9014 "%s", name);
9015 name = "";
9017 else
9018 for (i = 0; i < len; ++i)
9020 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9021 "%s", name);
9022 name = "";
9025 break;
9027 case dw_val_class_vec:
9029 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
9030 unsigned int len = a->dw_attr_val.v.val_vec.length;
9031 unsigned int i;
9032 unsigned char *p;
9034 dw2_asm_output_data (constant_size (len * elt_size),
9035 len * elt_size, "%s", name);
9036 if (elt_size > sizeof (HOST_WIDE_INT))
9038 elt_size /= 2;
9039 len *= 2;
9041 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
9042 i < len;
9043 i++, p += elt_size)
9044 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
9045 "fp or vector constant word %u", i);
9046 break;
9049 case dw_val_class_flag:
9050 if (dwarf_version >= 4)
9052 /* Currently all add_AT_flag calls pass in 1 as last argument,
9053 so DW_FORM_flag_present can be used. If that ever changes,
9054 we'll need to use DW_FORM_flag and have some optimization
9055 in build_abbrev_table that will change those to
9056 DW_FORM_flag_present if it is set to 1 in all DIEs using
9057 the same abbrev entry. */
9058 gcc_assert (AT_flag (a) == 1);
9059 if (flag_debug_asm)
9060 fprintf (asm_out_file, "\t\t\t%s %s\n",
9061 ASM_COMMENT_START, name);
9062 break;
9064 dw2_asm_output_data (1, AT_flag (a), "%s", name);
9065 break;
9067 case dw_val_class_loc_list:
9068 output_attr_index_or_value (a);
9069 break;
9071 case dw_val_class_die_ref:
9072 if (AT_ref_external (a))
9074 if (AT_ref (a)->comdat_type_p)
9076 comdat_type_node *type_node =
9077 AT_ref (a)->die_id.die_type_node;
9079 gcc_assert (type_node);
9080 output_signature (type_node->signature, name);
9082 else
9084 const char *sym = AT_ref (a)->die_id.die_symbol;
9085 int size;
9087 gcc_assert (sym);
9088 /* In DWARF2, DW_FORM_ref_addr is sized by target address
9089 length, whereas in DWARF3 it's always sized as an
9090 offset. */
9091 if (dwarf_version == 2)
9092 size = DWARF2_ADDR_SIZE;
9093 else
9094 size = DWARF_OFFSET_SIZE;
9095 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
9096 name);
9099 else
9101 gcc_assert (AT_ref (a)->die_offset);
9102 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
9103 "%s", name);
9105 break;
9107 case dw_val_class_fde_ref:
9109 char l1[20];
9111 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9112 a->dw_attr_val.v.val_fde_index * 2);
9113 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9114 "%s", name);
9116 break;
9118 case dw_val_class_vms_delta:
9119 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
9120 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9121 AT_vms_delta2 (a), AT_vms_delta1 (a),
9122 "%s", name);
9123 #else
9124 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
9125 AT_vms_delta2 (a), AT_vms_delta1 (a),
9126 "%s", name);
9127 #endif
9128 break;
9130 case dw_val_class_lbl_id:
9131 output_attr_index_or_value (a);
9132 break;
9134 case dw_val_class_lineptr:
9135 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9136 debug_line_section, "%s", name);
9137 break;
9139 case dw_val_class_macptr:
9140 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9141 debug_macinfo_section, "%s", name);
9142 break;
9144 case dw_val_class_str:
9145 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9146 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9147 a->dw_attr_val.v.val_str->label,
9148 debug_str_section,
9149 "%s: \"%s\"", name, AT_string (a));
9150 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9151 dw2_asm_output_data_uleb128 (AT_index (a),
9152 "%s: \"%s\"", name, AT_string (a));
9153 else
9154 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9155 break;
9157 case dw_val_class_file:
9159 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9161 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9162 a->dw_attr_val.v.val_file->filename);
9163 break;
9166 case dw_val_class_data8:
9168 int i;
9170 for (i = 0; i < 8; i++)
9171 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9172 i == 0 ? "%s" : NULL, name);
9173 break;
9176 case dw_val_class_high_pc:
9177 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9178 get_AT_low_pc (die), "DW_AT_high_pc");
9179 break;
9181 default:
9182 gcc_unreachable ();
9186 FOR_EACH_CHILD (die, c, output_die (c));
9188 /* Add null byte to terminate sibling list. */
9189 if (die->die_child != NULL)
9190 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9191 (unsigned long) die->die_offset);
9194 /* Output the compilation unit that appears at the beginning of the
9195 .debug_info section, and precedes the DIE descriptions. */
9197 static void
9198 output_compilation_unit_header (void)
9200 /* We don't support actual DWARFv5 units yet, we just use some
9201 DWARFv5 draft DIE tags in DWARFv4 format. */
9202 int ver = dwarf_version < 5 ? dwarf_version : 4;
9204 if (!XCOFF_DEBUGGING_INFO)
9206 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9207 dw2_asm_output_data (4, 0xffffffff,
9208 "Initial length escape value indicating 64-bit DWARF extension");
9209 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9210 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9211 "Length of Compilation Unit Info");
9214 dw2_asm_output_data (2, ver, "DWARF version number");
9215 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9216 debug_abbrev_section,
9217 "Offset Into Abbrev. Section");
9218 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9221 /* Output the compilation unit DIE and its children. */
9223 static void
9224 output_comp_unit (dw_die_ref die, int output_if_empty)
9226 const char *secname, *oldsym;
9227 char *tmp;
9229 /* Unless we are outputting main CU, we may throw away empty ones. */
9230 if (!output_if_empty && die->die_child == NULL)
9231 return;
9233 /* Even if there are no children of this DIE, we must output the information
9234 about the compilation unit. Otherwise, on an empty translation unit, we
9235 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
9236 will then complain when examining the file. First mark all the DIEs in
9237 this CU so we know which get local refs. */
9238 mark_dies (die);
9240 external_ref_hash_type *extern_map = optimize_external_refs (die);
9242 build_abbrev_table (die, extern_map);
9244 delete extern_map;
9246 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9247 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9248 calc_die_sizes (die);
9250 oldsym = die->die_id.die_symbol;
9251 if (oldsym)
9253 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9255 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9256 secname = tmp;
9257 die->die_id.die_symbol = NULL;
9258 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9260 else
9262 switch_to_section (debug_info_section);
9263 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9264 info_section_emitted = true;
9267 /* Output debugging information. */
9268 output_compilation_unit_header ();
9269 output_die (die);
9271 /* Leave the marks on the main CU, so we can check them in
9272 output_pubnames. */
9273 if (oldsym)
9275 unmark_dies (die);
9276 die->die_id.die_symbol = oldsym;
9280 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9281 and .debug_pubtypes. This is configured per-target, but can be
9282 overridden by the -gpubnames or -gno-pubnames options. */
9284 static inline bool
9285 want_pubnames (void)
9287 if (debug_info_level <= DINFO_LEVEL_TERSE)
9288 return false;
9289 if (debug_generate_pub_sections != -1)
9290 return debug_generate_pub_sections;
9291 return targetm.want_debug_pub_sections;
9294 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9296 static void
9297 add_AT_pubnames (dw_die_ref die)
9299 if (want_pubnames ())
9300 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9303 /* Add a string attribute value to a skeleton DIE. */
9305 static inline void
9306 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9307 const char *str)
9309 dw_attr_node attr;
9310 struct indirect_string_node *node;
9312 if (! skeleton_debug_str_hash)
9313 skeleton_debug_str_hash
9314 = hash_table<indirect_string_hasher>::create_ggc (10);
9316 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9317 find_string_form (node);
9318 if (node->form == DW_FORM_GNU_str_index)
9319 node->form = DW_FORM_strp;
9321 attr.dw_attr = attr_kind;
9322 attr.dw_attr_val.val_class = dw_val_class_str;
9323 attr.dw_attr_val.val_entry = NULL;
9324 attr.dw_attr_val.v.val_str = node;
9325 add_dwarf_attr (die, &attr);
9328 /* Helper function to generate top-level dies for skeleton debug_info and
9329 debug_types. */
9331 static void
9332 add_top_level_skeleton_die_attrs (dw_die_ref die)
9334 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9335 const char *comp_dir = comp_dir_string ();
9337 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9338 if (comp_dir != NULL)
9339 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9340 add_AT_pubnames (die);
9341 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9344 /* Output skeleton debug sections that point to the dwo file. */
9346 static void
9347 output_skeleton_debug_sections (dw_die_ref comp_unit)
9349 /* We don't support actual DWARFv5 units yet, we just use some
9350 DWARFv5 draft DIE tags in DWARFv4 format. */
9351 int ver = dwarf_version < 5 ? dwarf_version : 4;
9353 /* These attributes will be found in the full debug_info section. */
9354 remove_AT (comp_unit, DW_AT_producer);
9355 remove_AT (comp_unit, DW_AT_language);
9357 switch_to_section (debug_skeleton_info_section);
9358 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9360 /* Produce the skeleton compilation-unit header. This one differs enough from
9361 a normal CU header that it's better not to call output_compilation_unit
9362 header. */
9363 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9364 dw2_asm_output_data (4, 0xffffffff,
9365 "Initial length escape value indicating 64-bit DWARF extension");
9367 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9368 DWARF_COMPILE_UNIT_HEADER_SIZE
9369 - DWARF_INITIAL_LENGTH_SIZE
9370 + size_of_die (comp_unit),
9371 "Length of Compilation Unit Info");
9372 dw2_asm_output_data (2, ver, "DWARF version number");
9373 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9374 debug_abbrev_section,
9375 "Offset Into Abbrev. Section");
9376 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9378 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9379 output_die (comp_unit);
9381 /* Build the skeleton debug_abbrev section. */
9382 switch_to_section (debug_skeleton_abbrev_section);
9383 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9385 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9387 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9390 /* Output a comdat type unit DIE and its children. */
9392 static void
9393 output_comdat_type_unit (comdat_type_node *node)
9395 const char *secname;
9396 char *tmp;
9397 int i;
9398 #if defined (OBJECT_FORMAT_ELF)
9399 tree comdat_key;
9400 #endif
9402 /* First mark all the DIEs in this CU so we know which get local refs. */
9403 mark_dies (node->root_die);
9405 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9407 build_abbrev_table (node->root_die, extern_map);
9409 delete extern_map;
9410 extern_map = NULL;
9412 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9413 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9414 calc_die_sizes (node->root_die);
9416 #if defined (OBJECT_FORMAT_ELF)
9417 if (!dwarf_split_debug_info)
9418 secname = ".debug_types";
9419 else
9420 secname = ".debug_types.dwo";
9422 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9423 sprintf (tmp, "wt.");
9424 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9425 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9426 comdat_key = get_identifier (tmp);
9427 targetm.asm_out.named_section (secname,
9428 SECTION_DEBUG | SECTION_LINKONCE,
9429 comdat_key);
9430 #else
9431 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9432 sprintf (tmp, ".gnu.linkonce.wt.");
9433 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9434 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9435 secname = tmp;
9436 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9437 #endif
9439 /* Output debugging information. */
9440 output_compilation_unit_header ();
9441 output_signature (node->signature, "Type Signature");
9442 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9443 "Offset to Type DIE");
9444 output_die (node->root_die);
9446 unmark_dies (node->root_die);
9449 /* Return the DWARF2/3 pubname associated with a decl. */
9451 static const char *
9452 dwarf2_name (tree decl, int scope)
9454 if (DECL_NAMELESS (decl))
9455 return NULL;
9456 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9459 /* Add a new entry to .debug_pubnames if appropriate. */
9461 static void
9462 add_pubname_string (const char *str, dw_die_ref die)
9464 pubname_entry e;
9466 e.die = die;
9467 e.name = xstrdup (str);
9468 vec_safe_push (pubname_table, e);
9471 static void
9472 add_pubname (tree decl, dw_die_ref die)
9474 if (!want_pubnames ())
9475 return;
9477 /* Don't add items to the table when we expect that the consumer will have
9478 just read the enclosing die. For example, if the consumer is looking at a
9479 class_member, it will either be inside the class already, or will have just
9480 looked up the class to find the member. Either way, searching the class is
9481 faster than searching the index. */
9482 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9483 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9485 const char *name = dwarf2_name (decl, 1);
9487 if (name)
9488 add_pubname_string (name, die);
9492 /* Add an enumerator to the pubnames section. */
9494 static void
9495 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9497 pubname_entry e;
9499 gcc_assert (scope_name);
9500 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9501 e.die = die;
9502 vec_safe_push (pubname_table, e);
9505 /* Add a new entry to .debug_pubtypes if appropriate. */
9507 static void
9508 add_pubtype (tree decl, dw_die_ref die)
9510 pubname_entry e;
9512 if (!want_pubnames ())
9513 return;
9515 if ((TREE_PUBLIC (decl)
9516 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9517 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9519 tree scope = NULL;
9520 const char *scope_name = "";
9521 const char *sep = is_cxx () ? "::" : ".";
9522 const char *name;
9524 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9525 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9527 scope_name = lang_hooks.dwarf_name (scope, 1);
9528 if (scope_name != NULL && scope_name[0] != '\0')
9529 scope_name = concat (scope_name, sep, NULL);
9530 else
9531 scope_name = "";
9534 if (TYPE_P (decl))
9535 name = type_tag (decl);
9536 else
9537 name = lang_hooks.dwarf_name (decl, 1);
9539 /* If we don't have a name for the type, there's no point in adding
9540 it to the table. */
9541 if (name != NULL && name[0] != '\0')
9543 e.die = die;
9544 e.name = concat (scope_name, name, NULL);
9545 vec_safe_push (pubtype_table, e);
9548 /* Although it might be more consistent to add the pubinfo for the
9549 enumerators as their dies are created, they should only be added if the
9550 enum type meets the criteria above. So rather than re-check the parent
9551 enum type whenever an enumerator die is created, just output them all
9552 here. This isn't protected by the name conditional because anonymous
9553 enums don't have names. */
9554 if (die->die_tag == DW_TAG_enumeration_type)
9556 dw_die_ref c;
9558 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9563 /* Output a single entry in the pubnames table. */
9565 static void
9566 output_pubname (dw_offset die_offset, pubname_entry *entry)
9568 dw_die_ref die = entry->die;
9569 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9571 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9573 if (debug_generate_pub_sections == 2)
9575 /* This logic follows gdb's method for determining the value of the flag
9576 byte. */
9577 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9578 switch (die->die_tag)
9580 case DW_TAG_typedef:
9581 case DW_TAG_base_type:
9582 case DW_TAG_subrange_type:
9583 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9584 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9585 break;
9586 case DW_TAG_enumerator:
9587 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9588 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9589 if (!is_cxx () && !is_java ())
9590 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9591 break;
9592 case DW_TAG_subprogram:
9593 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9594 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9595 if (!is_ada ())
9596 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9597 break;
9598 case DW_TAG_constant:
9599 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9600 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9601 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9602 break;
9603 case DW_TAG_variable:
9604 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9605 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9606 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9607 break;
9608 case DW_TAG_namespace:
9609 case DW_TAG_imported_declaration:
9610 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9611 break;
9612 case DW_TAG_class_type:
9613 case DW_TAG_interface_type:
9614 case DW_TAG_structure_type:
9615 case DW_TAG_union_type:
9616 case DW_TAG_enumeration_type:
9617 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9618 if (!is_cxx () && !is_java ())
9619 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9620 break;
9621 default:
9622 /* An unusual tag. Leave the flag-byte empty. */
9623 break;
9625 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9626 "GDB-index flags");
9629 dw2_asm_output_nstring (entry->name, -1, "external name");
9633 /* Output the public names table used to speed up access to externally
9634 visible names; or the public types table used to find type definitions. */
9636 static void
9637 output_pubnames (vec<pubname_entry, va_gc> *names)
9639 unsigned i;
9640 unsigned long pubnames_length = size_of_pubnames (names);
9641 pubname_entry *pub;
9643 if (!XCOFF_DEBUGGING_INFO)
9645 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9646 dw2_asm_output_data (4, 0xffffffff,
9647 "Initial length escape value indicating 64-bit DWARF extension");
9648 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9649 "Pub Info Length");
9652 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9653 dw2_asm_output_data (2, 2, "DWARF Version");
9655 if (dwarf_split_debug_info)
9656 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9657 debug_skeleton_info_section,
9658 "Offset of Compilation Unit Info");
9659 else
9660 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9661 debug_info_section,
9662 "Offset of Compilation Unit Info");
9663 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9664 "Compilation Unit Length");
9666 FOR_EACH_VEC_ELT (*names, i, pub)
9668 if (include_pubname_in_output (names, pub))
9670 dw_offset die_offset = pub->die->die_offset;
9672 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9673 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9674 gcc_assert (pub->die->die_mark);
9676 /* If we're putting types in their own .debug_types sections,
9677 the .debug_pubtypes table will still point to the compile
9678 unit (not the type unit), so we want to use the offset of
9679 the skeleton DIE (if there is one). */
9680 if (pub->die->comdat_type_p && names == pubtype_table)
9682 comdat_type_node *type_node = pub->die->die_id.die_type_node;
9684 if (type_node != NULL)
9685 die_offset = (type_node->skeleton_die != NULL
9686 ? type_node->skeleton_die->die_offset
9687 : comp_unit_die ()->die_offset);
9690 output_pubname (die_offset, pub);
9694 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9697 /* Output public names and types tables if necessary. */
9699 static void
9700 output_pubtables (void)
9702 if (!want_pubnames () || !info_section_emitted)
9703 return;
9705 switch_to_section (debug_pubnames_section);
9706 output_pubnames (pubname_table);
9707 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9708 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9709 simply won't look for the section. */
9710 switch_to_section (debug_pubtypes_section);
9711 output_pubnames (pubtype_table);
9715 /* Output the information that goes into the .debug_aranges table.
9716 Namely, define the beginning and ending address range of the
9717 text section generated for this compilation unit. */
9719 static void
9720 output_aranges (void)
9722 unsigned i;
9723 unsigned long aranges_length = size_of_aranges ();
9725 if (!XCOFF_DEBUGGING_INFO)
9727 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9728 dw2_asm_output_data (4, 0xffffffff,
9729 "Initial length escape value indicating 64-bit DWARF extension");
9730 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9731 "Length of Address Ranges Info");
9734 /* Version number for aranges is still 2, even up to DWARF5. */
9735 dw2_asm_output_data (2, 2, "DWARF Version");
9736 if (dwarf_split_debug_info)
9737 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9738 debug_skeleton_info_section,
9739 "Offset of Compilation Unit Info");
9740 else
9741 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9742 debug_info_section,
9743 "Offset of Compilation Unit Info");
9744 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9745 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9747 /* We need to align to twice the pointer size here. */
9748 if (DWARF_ARANGES_PAD_SIZE)
9750 /* Pad using a 2 byte words so that padding is correct for any
9751 pointer size. */
9752 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9753 2 * DWARF2_ADDR_SIZE);
9754 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9755 dw2_asm_output_data (2, 0, NULL);
9758 /* It is necessary not to output these entries if the sections were
9759 not used; if the sections were not used, the length will be 0 and
9760 the address may end up as 0 if the section is discarded by ld
9761 --gc-sections, leaving an invalid (0, 0) entry that can be
9762 confused with the terminator. */
9763 if (text_section_used)
9765 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9766 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9767 text_section_label, "Length");
9769 if (cold_text_section_used)
9771 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9772 "Address");
9773 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9774 cold_text_section_label, "Length");
9777 if (have_multiple_function_sections)
9779 unsigned fde_idx;
9780 dw_fde_ref fde;
9782 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9784 if (DECL_IGNORED_P (fde->decl))
9785 continue;
9786 if (!fde->in_std_section)
9788 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9789 "Address");
9790 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9791 fde->dw_fde_begin, "Length");
9793 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9795 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9796 "Address");
9797 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9798 fde->dw_fde_second_begin, "Length");
9803 /* Output the terminator words. */
9804 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9805 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9808 /* Add a new entry to .debug_ranges. Return the offset at which it
9809 was placed. */
9811 static unsigned int
9812 add_ranges_num (int num)
9814 unsigned int in_use = ranges_table_in_use;
9816 if (in_use == ranges_table_allocated)
9818 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9819 ranges_table = GGC_RESIZEVEC (dw_ranges, ranges_table,
9820 ranges_table_allocated);
9821 memset (ranges_table + ranges_table_in_use, 0,
9822 RANGES_TABLE_INCREMENT * sizeof (dw_ranges));
9825 ranges_table[in_use].num = num;
9826 ranges_table_in_use = in_use + 1;
9828 return in_use * 2 * DWARF2_ADDR_SIZE;
9831 /* Add a new entry to .debug_ranges corresponding to a block, or a
9832 range terminator if BLOCK is NULL. */
9834 static unsigned int
9835 add_ranges (const_tree block)
9837 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9840 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9841 When using dwarf_split_debug_info, address attributes in dies destined
9842 for the final executable should be direct references--setting the
9843 parameter force_direct ensures this behavior. */
9845 static void
9846 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9847 bool *added, bool force_direct)
9849 unsigned int in_use = ranges_by_label_in_use;
9850 unsigned int offset;
9852 if (in_use == ranges_by_label_allocated)
9854 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9855 ranges_by_label = GGC_RESIZEVEC (dw_ranges_by_label, ranges_by_label,
9856 ranges_by_label_allocated);
9857 memset (ranges_by_label + ranges_by_label_in_use, 0,
9858 RANGES_TABLE_INCREMENT * sizeof (dw_ranges_by_label));
9861 ranges_by_label[in_use].begin = begin;
9862 ranges_by_label[in_use].end = end;
9863 ranges_by_label_in_use = in_use + 1;
9865 offset = add_ranges_num (-(int)in_use - 1);
9866 if (!*added)
9868 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9869 *added = true;
9873 static void
9874 output_ranges (void)
9876 unsigned i;
9877 static const char *const start_fmt = "Offset %#x";
9878 const char *fmt = start_fmt;
9880 for (i = 0; i < ranges_table_in_use; i++)
9882 int block_num = ranges_table[i].num;
9884 if (block_num > 0)
9886 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9887 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9889 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9890 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9892 /* If all code is in the text section, then the compilation
9893 unit base address defaults to DW_AT_low_pc, which is the
9894 base of the text section. */
9895 if (!have_multiple_function_sections)
9897 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9898 text_section_label,
9899 fmt, i * 2 * DWARF2_ADDR_SIZE);
9900 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9901 text_section_label, NULL);
9904 /* Otherwise, the compilation unit base address is zero,
9905 which allows us to use absolute addresses, and not worry
9906 about whether the target supports cross-section
9907 arithmetic. */
9908 else
9910 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9911 fmt, i * 2 * DWARF2_ADDR_SIZE);
9912 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9915 fmt = NULL;
9918 /* Negative block_num stands for an index into ranges_by_label. */
9919 else if (block_num < 0)
9921 int lab_idx = - block_num - 1;
9923 if (!have_multiple_function_sections)
9925 gcc_unreachable ();
9926 #if 0
9927 /* If we ever use add_ranges_by_labels () for a single
9928 function section, all we have to do is to take out
9929 the #if 0 above. */
9930 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9931 ranges_by_label[lab_idx].begin,
9932 text_section_label,
9933 fmt, i * 2 * DWARF2_ADDR_SIZE);
9934 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9935 ranges_by_label[lab_idx].end,
9936 text_section_label, NULL);
9937 #endif
9939 else
9941 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9942 ranges_by_label[lab_idx].begin,
9943 fmt, i * 2 * DWARF2_ADDR_SIZE);
9944 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9945 ranges_by_label[lab_idx].end,
9946 NULL);
9949 else
9951 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9952 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9953 fmt = start_fmt;
9958 /* Data structure containing information about input files. */
9959 struct file_info
9961 const char *path; /* Complete file name. */
9962 const char *fname; /* File name part. */
9963 int length; /* Length of entire string. */
9964 struct dwarf_file_data * file_idx; /* Index in input file table. */
9965 int dir_idx; /* Index in directory table. */
9968 /* Data structure containing information about directories with source
9969 files. */
9970 struct dir_info
9972 const char *path; /* Path including directory name. */
9973 int length; /* Path length. */
9974 int prefix; /* Index of directory entry which is a prefix. */
9975 int count; /* Number of files in this directory. */
9976 int dir_idx; /* Index of directory used as base. */
9979 /* Callback function for file_info comparison. We sort by looking at
9980 the directories in the path. */
9982 static int
9983 file_info_cmp (const void *p1, const void *p2)
9985 const struct file_info *const s1 = (const struct file_info *) p1;
9986 const struct file_info *const s2 = (const struct file_info *) p2;
9987 const unsigned char *cp1;
9988 const unsigned char *cp2;
9990 /* Take care of file names without directories. We need to make sure that
9991 we return consistent values to qsort since some will get confused if
9992 we return the same value when identical operands are passed in opposite
9993 orders. So if neither has a directory, return 0 and otherwise return
9994 1 or -1 depending on which one has the directory. */
9995 if ((s1->path == s1->fname || s2->path == s2->fname))
9996 return (s2->path == s2->fname) - (s1->path == s1->fname);
9998 cp1 = (const unsigned char *) s1->path;
9999 cp2 = (const unsigned char *) s2->path;
10001 while (1)
10003 ++cp1;
10004 ++cp2;
10005 /* Reached the end of the first path? If so, handle like above. */
10006 if ((cp1 == (const unsigned char *) s1->fname)
10007 || (cp2 == (const unsigned char *) s2->fname))
10008 return ((cp2 == (const unsigned char *) s2->fname)
10009 - (cp1 == (const unsigned char *) s1->fname));
10011 /* Character of current path component the same? */
10012 else if (*cp1 != *cp2)
10013 return *cp1 - *cp2;
10017 struct file_name_acquire_data
10019 struct file_info *files;
10020 int used_files;
10021 int max_files;
10024 /* Traversal function for the hash table. */
10027 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
10029 struct dwarf_file_data *d = *slot;
10030 struct file_info *fi;
10031 const char *f;
10033 gcc_assert (fnad->max_files >= d->emitted_number);
10035 if (! d->emitted_number)
10036 return 1;
10038 gcc_assert (fnad->max_files != fnad->used_files);
10040 fi = fnad->files + fnad->used_files++;
10042 /* Skip all leading "./". */
10043 f = d->filename;
10044 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
10045 f += 2;
10047 /* Create a new array entry. */
10048 fi->path = f;
10049 fi->length = strlen (f);
10050 fi->file_idx = d;
10052 /* Search for the file name part. */
10053 f = strrchr (f, DIR_SEPARATOR);
10054 #if defined (DIR_SEPARATOR_2)
10056 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
10058 if (g != NULL)
10060 if (f == NULL || f < g)
10061 f = g;
10064 #endif
10066 fi->fname = f == NULL ? fi->path : f + 1;
10067 return 1;
10070 /* Output the directory table and the file name table. We try to minimize
10071 the total amount of memory needed. A heuristic is used to avoid large
10072 slowdowns with many input files. */
10074 static void
10075 output_file_names (void)
10077 struct file_name_acquire_data fnad;
10078 int numfiles;
10079 struct file_info *files;
10080 struct dir_info *dirs;
10081 int *saved;
10082 int *savehere;
10083 int *backmap;
10084 int ndirs;
10085 int idx_offset;
10086 int i;
10088 if (!last_emitted_file)
10090 dw2_asm_output_data (1, 0, "End directory table");
10091 dw2_asm_output_data (1, 0, "End file name table");
10092 return;
10095 numfiles = last_emitted_file->emitted_number;
10097 /* Allocate the various arrays we need. */
10098 files = XALLOCAVEC (struct file_info, numfiles);
10099 dirs = XALLOCAVEC (struct dir_info, numfiles);
10101 fnad.files = files;
10102 fnad.used_files = 0;
10103 fnad.max_files = numfiles;
10104 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
10105 gcc_assert (fnad.used_files == fnad.max_files);
10107 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
10109 /* Find all the different directories used. */
10110 dirs[0].path = files[0].path;
10111 dirs[0].length = files[0].fname - files[0].path;
10112 dirs[0].prefix = -1;
10113 dirs[0].count = 1;
10114 dirs[0].dir_idx = 0;
10115 files[0].dir_idx = 0;
10116 ndirs = 1;
10118 for (i = 1; i < numfiles; i++)
10119 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
10120 && memcmp (dirs[ndirs - 1].path, files[i].path,
10121 dirs[ndirs - 1].length) == 0)
10123 /* Same directory as last entry. */
10124 files[i].dir_idx = ndirs - 1;
10125 ++dirs[ndirs - 1].count;
10127 else
10129 int j;
10131 /* This is a new directory. */
10132 dirs[ndirs].path = files[i].path;
10133 dirs[ndirs].length = files[i].fname - files[i].path;
10134 dirs[ndirs].count = 1;
10135 dirs[ndirs].dir_idx = ndirs;
10136 files[i].dir_idx = ndirs;
10138 /* Search for a prefix. */
10139 dirs[ndirs].prefix = -1;
10140 for (j = 0; j < ndirs; j++)
10141 if (dirs[j].length < dirs[ndirs].length
10142 && dirs[j].length > 1
10143 && (dirs[ndirs].prefix == -1
10144 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10145 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10146 dirs[ndirs].prefix = j;
10148 ++ndirs;
10151 /* Now to the actual work. We have to find a subset of the directories which
10152 allow expressing the file name using references to the directory table
10153 with the least amount of characters. We do not do an exhaustive search
10154 where we would have to check out every combination of every single
10155 possible prefix. Instead we use a heuristic which provides nearly optimal
10156 results in most cases and never is much off. */
10157 saved = XALLOCAVEC (int, ndirs);
10158 savehere = XALLOCAVEC (int, ndirs);
10160 memset (saved, '\0', ndirs * sizeof (saved[0]));
10161 for (i = 0; i < ndirs; i++)
10163 int j;
10164 int total;
10166 /* We can always save some space for the current directory. But this
10167 does not mean it will be enough to justify adding the directory. */
10168 savehere[i] = dirs[i].length;
10169 total = (savehere[i] - saved[i]) * dirs[i].count;
10171 for (j = i + 1; j < ndirs; j++)
10173 savehere[j] = 0;
10174 if (saved[j] < dirs[i].length)
10176 /* Determine whether the dirs[i] path is a prefix of the
10177 dirs[j] path. */
10178 int k;
10180 k = dirs[j].prefix;
10181 while (k != -1 && k != (int) i)
10182 k = dirs[k].prefix;
10184 if (k == (int) i)
10186 /* Yes it is. We can possibly save some memory by
10187 writing the filenames in dirs[j] relative to
10188 dirs[i]. */
10189 savehere[j] = dirs[i].length;
10190 total += (savehere[j] - saved[j]) * dirs[j].count;
10195 /* Check whether we can save enough to justify adding the dirs[i]
10196 directory. */
10197 if (total > dirs[i].length + 1)
10199 /* It's worthwhile adding. */
10200 for (j = i; j < ndirs; j++)
10201 if (savehere[j] > 0)
10203 /* Remember how much we saved for this directory so far. */
10204 saved[j] = savehere[j];
10206 /* Remember the prefix directory. */
10207 dirs[j].dir_idx = i;
10212 /* Emit the directory name table. */
10213 idx_offset = dirs[0].length > 0 ? 1 : 0;
10214 for (i = 1 - idx_offset; i < ndirs; i++)
10215 dw2_asm_output_nstring (dirs[i].path,
10216 dirs[i].length
10217 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10218 "Directory Entry: %#x", i + idx_offset);
10220 dw2_asm_output_data (1, 0, "End directory table");
10222 /* We have to emit them in the order of emitted_number since that's
10223 used in the debug info generation. To do this efficiently we
10224 generate a back-mapping of the indices first. */
10225 backmap = XALLOCAVEC (int, numfiles);
10226 for (i = 0; i < numfiles; i++)
10227 backmap[files[i].file_idx->emitted_number - 1] = i;
10229 /* Now write all the file names. */
10230 for (i = 0; i < numfiles; i++)
10232 int file_idx = backmap[i];
10233 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10235 #ifdef VMS_DEBUGGING_INFO
10236 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10238 /* Setting these fields can lead to debugger miscomparisons,
10239 but VMS Debug requires them to be set correctly. */
10241 int ver;
10242 long long cdt;
10243 long siz;
10244 int maxfilelen = strlen (files[file_idx].path)
10245 + dirs[dir_idx].length
10246 + MAX_VMS_VERSION_LEN + 1;
10247 char *filebuf = XALLOCAVEC (char, maxfilelen);
10249 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10250 snprintf (filebuf, maxfilelen, "%s;%d",
10251 files[file_idx].path + dirs[dir_idx].length, ver);
10253 dw2_asm_output_nstring
10254 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10256 /* Include directory index. */
10257 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10259 /* Modification time. */
10260 dw2_asm_output_data_uleb128
10261 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10262 ? cdt : 0,
10263 NULL);
10265 /* File length in bytes. */
10266 dw2_asm_output_data_uleb128
10267 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10268 ? siz : 0,
10269 NULL);
10270 #else
10271 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10272 "File Entry: %#x", (unsigned) i + 1);
10274 /* Include directory index. */
10275 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10277 /* Modification time. */
10278 dw2_asm_output_data_uleb128 (0, NULL);
10280 /* File length in bytes. */
10281 dw2_asm_output_data_uleb128 (0, NULL);
10282 #endif /* VMS_DEBUGGING_INFO */
10285 dw2_asm_output_data (1, 0, "End file name table");
10289 /* Output one line number table into the .debug_line section. */
10291 static void
10292 output_one_line_info_table (dw_line_info_table *table)
10294 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10295 unsigned int current_line = 1;
10296 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10297 dw_line_info_entry *ent;
10298 size_t i;
10300 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10302 switch (ent->opcode)
10304 case LI_set_address:
10305 /* ??? Unfortunately, we have little choice here currently, and
10306 must always use the most general form. GCC does not know the
10307 address delta itself, so we can't use DW_LNS_advance_pc. Many
10308 ports do have length attributes which will give an upper bound
10309 on the address range. We could perhaps use length attributes
10310 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10311 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10313 /* This can handle any delta. This takes
10314 4+DWARF2_ADDR_SIZE bytes. */
10315 dw2_asm_output_data (1, 0, "set address %s", line_label);
10316 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10317 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10318 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10319 break;
10321 case LI_set_line:
10322 if (ent->val == current_line)
10324 /* We still need to start a new row, so output a copy insn. */
10325 dw2_asm_output_data (1, DW_LNS_copy,
10326 "copy line %u", current_line);
10328 else
10330 int line_offset = ent->val - current_line;
10331 int line_delta = line_offset - DWARF_LINE_BASE;
10333 current_line = ent->val;
10334 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10336 /* This can handle deltas from -10 to 234, using the current
10337 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10338 This takes 1 byte. */
10339 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10340 "line %u", current_line);
10342 else
10344 /* This can handle any delta. This takes at least 4 bytes,
10345 depending on the value being encoded. */
10346 dw2_asm_output_data (1, DW_LNS_advance_line,
10347 "advance to line %u", current_line);
10348 dw2_asm_output_data_sleb128 (line_offset, NULL);
10349 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10352 break;
10354 case LI_set_file:
10355 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10356 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10357 break;
10359 case LI_set_column:
10360 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10361 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10362 break;
10364 case LI_negate_stmt:
10365 current_is_stmt = !current_is_stmt;
10366 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10367 "is_stmt %d", current_is_stmt);
10368 break;
10370 case LI_set_prologue_end:
10371 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10372 "set prologue end");
10373 break;
10375 case LI_set_epilogue_begin:
10376 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10377 "set epilogue begin");
10378 break;
10380 case LI_set_discriminator:
10381 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10382 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10383 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10384 dw2_asm_output_data_uleb128 (ent->val, NULL);
10385 break;
10389 /* Emit debug info for the address of the end of the table. */
10390 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10391 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10392 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10393 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10395 dw2_asm_output_data (1, 0, "end sequence");
10396 dw2_asm_output_data_uleb128 (1, NULL);
10397 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10400 /* Output the source line number correspondence information. This
10401 information goes into the .debug_line section. */
10403 static void
10404 output_line_info (bool prologue_only)
10406 char l1[20], l2[20], p1[20], p2[20];
10407 /* We don't support DWARFv5 line tables yet. */
10408 int ver = dwarf_version < 5 ? dwarf_version : 4;
10409 bool saw_one = false;
10410 int opc;
10412 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10413 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10414 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10415 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10417 if (!XCOFF_DEBUGGING_INFO)
10419 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10420 dw2_asm_output_data (4, 0xffffffff,
10421 "Initial length escape value indicating 64-bit DWARF extension");
10422 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10423 "Length of Source Line Info");
10426 ASM_OUTPUT_LABEL (asm_out_file, l1);
10428 dw2_asm_output_data (2, ver, "DWARF Version");
10429 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10430 ASM_OUTPUT_LABEL (asm_out_file, p1);
10432 /* Define the architecture-dependent minimum instruction length (in bytes).
10433 In this implementation of DWARF, this field is used for information
10434 purposes only. Since GCC generates assembly language, we have no
10435 a priori knowledge of how many instruction bytes are generated for each
10436 source line, and therefore can use only the DW_LNE_set_address and
10437 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10438 this as '1', which is "correct enough" for all architectures,
10439 and don't let the target override. */
10440 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10442 if (ver >= 4)
10443 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10444 "Maximum Operations Per Instruction");
10445 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10446 "Default is_stmt_start flag");
10447 dw2_asm_output_data (1, DWARF_LINE_BASE,
10448 "Line Base Value (Special Opcodes)");
10449 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10450 "Line Range Value (Special Opcodes)");
10451 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10452 "Special Opcode Base");
10454 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10456 int n_op_args;
10457 switch (opc)
10459 case DW_LNS_advance_pc:
10460 case DW_LNS_advance_line:
10461 case DW_LNS_set_file:
10462 case DW_LNS_set_column:
10463 case DW_LNS_fixed_advance_pc:
10464 case DW_LNS_set_isa:
10465 n_op_args = 1;
10466 break;
10467 default:
10468 n_op_args = 0;
10469 break;
10472 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10473 opc, n_op_args);
10476 /* Write out the information about the files we use. */
10477 output_file_names ();
10478 ASM_OUTPUT_LABEL (asm_out_file, p2);
10479 if (prologue_only)
10481 /* Output the marker for the end of the line number info. */
10482 ASM_OUTPUT_LABEL (asm_out_file, l2);
10483 return;
10486 if (separate_line_info)
10488 dw_line_info_table *table;
10489 size_t i;
10491 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10492 if (table->in_use)
10494 output_one_line_info_table (table);
10495 saw_one = true;
10498 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10500 output_one_line_info_table (cold_text_section_line_info);
10501 saw_one = true;
10504 /* ??? Some Darwin linkers crash on a .debug_line section with no
10505 sequences. Further, merely a DW_LNE_end_sequence entry is not
10506 sufficient -- the address column must also be initialized.
10507 Make sure to output at least one set_address/end_sequence pair,
10508 choosing .text since that section is always present. */
10509 if (text_section_line_info->in_use || !saw_one)
10510 output_one_line_info_table (text_section_line_info);
10512 /* Output the marker for the end of the line number info. */
10513 ASM_OUTPUT_LABEL (asm_out_file, l2);
10516 /* Given a pointer to a tree node for some base type, return a pointer to
10517 a DIE that describes the given type.
10519 This routine must only be called for GCC type nodes that correspond to
10520 Dwarf base (fundamental) types. */
10522 static dw_die_ref
10523 base_type_die (tree type)
10525 dw_die_ref base_type_result;
10526 enum dwarf_type encoding;
10528 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10529 return 0;
10531 /* If this is a subtype that should not be emitted as a subrange type,
10532 use the base type. See subrange_type_for_debug_p. */
10533 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10534 type = TREE_TYPE (type);
10536 switch (TREE_CODE (type))
10538 case INTEGER_TYPE:
10539 if ((dwarf_version >= 4 || !dwarf_strict)
10540 && TYPE_NAME (type)
10541 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10542 && DECL_IS_BUILTIN (TYPE_NAME (type))
10543 && DECL_NAME (TYPE_NAME (type)))
10545 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10546 if (strcmp (name, "char16_t") == 0
10547 || strcmp (name, "char32_t") == 0)
10549 encoding = DW_ATE_UTF;
10550 break;
10553 if (TYPE_STRING_FLAG (type))
10555 if (TYPE_UNSIGNED (type))
10556 encoding = DW_ATE_unsigned_char;
10557 else
10558 encoding = DW_ATE_signed_char;
10560 else if (TYPE_UNSIGNED (type))
10561 encoding = DW_ATE_unsigned;
10562 else
10563 encoding = DW_ATE_signed;
10564 break;
10566 case REAL_TYPE:
10567 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10569 if (dwarf_version >= 3 || !dwarf_strict)
10570 encoding = DW_ATE_decimal_float;
10571 else
10572 encoding = DW_ATE_lo_user;
10574 else
10575 encoding = DW_ATE_float;
10576 break;
10578 case FIXED_POINT_TYPE:
10579 if (!(dwarf_version >= 3 || !dwarf_strict))
10580 encoding = DW_ATE_lo_user;
10581 else if (TYPE_UNSIGNED (type))
10582 encoding = DW_ATE_unsigned_fixed;
10583 else
10584 encoding = DW_ATE_signed_fixed;
10585 break;
10587 /* Dwarf2 doesn't know anything about complex ints, so use
10588 a user defined type for it. */
10589 case COMPLEX_TYPE:
10590 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10591 encoding = DW_ATE_complex_float;
10592 else
10593 encoding = DW_ATE_lo_user;
10594 break;
10596 case BOOLEAN_TYPE:
10597 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10598 encoding = DW_ATE_boolean;
10599 break;
10601 default:
10602 /* No other TREE_CODEs are Dwarf fundamental types. */
10603 gcc_unreachable ();
10606 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10608 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10609 int_size_in_bytes (type));
10610 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10611 add_pubtype (type, base_type_result);
10613 return base_type_result;
10616 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10617 named 'auto' in its type: return true for it, false otherwise. */
10619 static inline bool
10620 is_cxx_auto (tree type)
10622 if (is_cxx ())
10624 tree name = TYPE_IDENTIFIER (type);
10625 if (name == get_identifier ("auto")
10626 || name == get_identifier ("decltype(auto)"))
10627 return true;
10629 return false;
10632 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10633 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10635 static inline int
10636 is_base_type (tree type)
10638 switch (TREE_CODE (type))
10640 case ERROR_MARK:
10641 case VOID_TYPE:
10642 case INTEGER_TYPE:
10643 case REAL_TYPE:
10644 case FIXED_POINT_TYPE:
10645 case COMPLEX_TYPE:
10646 case BOOLEAN_TYPE:
10647 case POINTER_BOUNDS_TYPE:
10648 return 1;
10650 case ARRAY_TYPE:
10651 case RECORD_TYPE:
10652 case UNION_TYPE:
10653 case QUAL_UNION_TYPE:
10654 case ENUMERAL_TYPE:
10655 case FUNCTION_TYPE:
10656 case METHOD_TYPE:
10657 case POINTER_TYPE:
10658 case REFERENCE_TYPE:
10659 case NULLPTR_TYPE:
10660 case OFFSET_TYPE:
10661 case LANG_TYPE:
10662 case VECTOR_TYPE:
10663 return 0;
10665 default:
10666 if (is_cxx_auto (type))
10667 return 0;
10668 gcc_unreachable ();
10671 return 0;
10674 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10675 node, return the size in bits for the type if it is a constant, or else
10676 return the alignment for the type if the type's size is not constant, or
10677 else return BITS_PER_WORD if the type actually turns out to be an
10678 ERROR_MARK node. */
10680 static inline unsigned HOST_WIDE_INT
10681 simple_type_size_in_bits (const_tree type)
10683 if (TREE_CODE (type) == ERROR_MARK)
10684 return BITS_PER_WORD;
10685 else if (TYPE_SIZE (type) == NULL_TREE)
10686 return 0;
10687 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10688 return tree_to_uhwi (TYPE_SIZE (type));
10689 else
10690 return TYPE_ALIGN (type);
10693 /* Similarly, but return an offset_int instead of UHWI. */
10695 static inline offset_int
10696 offset_int_type_size_in_bits (const_tree type)
10698 if (TREE_CODE (type) == ERROR_MARK)
10699 return BITS_PER_WORD;
10700 else if (TYPE_SIZE (type) == NULL_TREE)
10701 return 0;
10702 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10703 return wi::to_offset (TYPE_SIZE (type));
10704 else
10705 return TYPE_ALIGN (type);
10708 /* Given a pointer to a tree node for a subrange type, return a pointer
10709 to a DIE that describes the given type. */
10711 static dw_die_ref
10712 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10714 dw_die_ref subrange_die;
10715 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10717 if (context_die == NULL)
10718 context_die = comp_unit_die ();
10720 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10722 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10724 /* The size of the subrange type and its base type do not match,
10725 so we need to generate a size attribute for the subrange type. */
10726 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10729 if (low)
10730 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
10731 if (high)
10732 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
10734 return subrange_die;
10737 /* Returns the (const and/or volatile) cv_qualifiers associated with
10738 the decl node. This will normally be augmented with the
10739 cv_qualifiers of the underlying type in add_type_attribute. */
10741 static int
10742 decl_quals (const_tree decl)
10744 return ((TREE_READONLY (decl)
10745 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10746 | (TREE_THIS_VOLATILE (decl)
10747 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10750 /* Determine the TYPE whose qualifiers match the largest strict subset
10751 of the given TYPE_QUALS, and return its qualifiers. Ignore all
10752 qualifiers outside QUAL_MASK. */
10754 static int
10755 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10757 tree t;
10758 int best_rank = 0, best_qual = 0, max_rank;
10760 type_quals &= qual_mask;
10761 max_rank = popcount_hwi (type_quals) - 1;
10763 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10764 t = TYPE_NEXT_VARIANT (t))
10766 int q = TYPE_QUALS (t) & qual_mask;
10768 if ((q & type_quals) == q && q != type_quals
10769 && check_base_type (t, type))
10771 int rank = popcount_hwi (q);
10773 if (rank > best_rank)
10775 best_rank = rank;
10776 best_qual = q;
10781 return best_qual;
10784 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10785 entry that chains various modifiers in front of the given type. */
10787 static dw_die_ref
10788 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10790 enum tree_code code = TREE_CODE (type);
10791 dw_die_ref mod_type_die;
10792 dw_die_ref sub_die = NULL;
10793 tree item_type = NULL;
10794 tree qualified_type;
10795 tree name, low, high;
10796 dw_die_ref mod_scope;
10797 /* Only these cv-qualifiers are currently handled. */
10798 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10799 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
10801 if (code == ERROR_MARK)
10802 return NULL;
10804 cv_quals &= cv_qual_mask;
10806 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10807 tag modifier (and not an attribute) old consumers won't be able
10808 to handle it. */
10809 if (dwarf_version < 3)
10810 cv_quals &= ~TYPE_QUAL_RESTRICT;
10812 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
10813 if (dwarf_version < 5)
10814 cv_quals &= ~TYPE_QUAL_ATOMIC;
10816 /* See if we already have the appropriately qualified variant of
10817 this type. */
10818 qualified_type = get_qualified_type (type, cv_quals);
10820 if (qualified_type == sizetype
10821 && TYPE_NAME (qualified_type)
10822 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10824 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10826 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10827 && TYPE_PRECISION (t)
10828 == TYPE_PRECISION (qualified_type)
10829 && TYPE_UNSIGNED (t)
10830 == TYPE_UNSIGNED (qualified_type));
10831 qualified_type = t;
10834 /* If we do, then we can just use its DIE, if it exists. */
10835 if (qualified_type)
10837 mod_type_die = lookup_type_die (qualified_type);
10838 if (mod_type_die)
10839 return mod_type_die;
10842 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10844 /* Handle C typedef types. */
10845 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10846 && !DECL_ARTIFICIAL (name))
10848 tree dtype = TREE_TYPE (name);
10850 if (qualified_type == dtype)
10852 /* For a named type, use the typedef. */
10853 gen_type_die (qualified_type, context_die);
10854 return lookup_type_die (qualified_type);
10856 else
10858 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10859 dquals &= cv_qual_mask;
10860 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10861 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10862 /* cv-unqualified version of named type. Just use
10863 the unnamed type to which it refers. */
10864 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10865 cv_quals, context_die);
10866 /* Else cv-qualified version of named type; fall through. */
10870 mod_scope = scope_die_for (type, context_die);
10872 if (cv_quals)
10874 struct qual_info { int q; enum dwarf_tag t; };
10875 static const struct qual_info qual_info[] =
10877 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type },
10878 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10879 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10880 { TYPE_QUAL_CONST, DW_TAG_const_type },
10882 int sub_quals;
10883 unsigned i;
10885 /* Determine a lesser qualified type that most closely matches
10886 this one. Then generate DW_TAG_* entries for the remaining
10887 qualifiers. */
10888 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10889 cv_qual_mask);
10890 mod_type_die = modified_type_die (type, sub_quals, context_die);
10892 for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10893 if (qual_info[i].q & cv_quals & ~sub_quals)
10895 dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10896 if (mod_type_die)
10897 add_AT_die_ref (d, DW_AT_type, mod_type_die);
10898 mod_type_die = d;
10901 else if (code == POINTER_TYPE)
10903 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10904 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10905 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10906 item_type = TREE_TYPE (type);
10907 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10908 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10909 TYPE_ADDR_SPACE (item_type));
10911 else if (code == REFERENCE_TYPE)
10913 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10914 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10915 type);
10916 else
10917 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10918 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10919 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10920 item_type = TREE_TYPE (type);
10921 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10922 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10923 TYPE_ADDR_SPACE (item_type));
10925 else if (code == INTEGER_TYPE
10926 && TREE_TYPE (type) != NULL_TREE
10927 && subrange_type_for_debug_p (type, &low, &high))
10929 mod_type_die = subrange_type_die (type, low, high, context_die);
10930 item_type = TREE_TYPE (type);
10932 else if (is_base_type (type))
10933 mod_type_die = base_type_die (type);
10934 else
10936 gen_type_die (type, context_die);
10938 /* We have to get the type_main_variant here (and pass that to the
10939 `lookup_type_die' routine) because the ..._TYPE node we have
10940 might simply be a *copy* of some original type node (where the
10941 copy was created to help us keep track of typedef names) and
10942 that copy might have a different TYPE_UID from the original
10943 ..._TYPE node. */
10944 if (TREE_CODE (type) != VECTOR_TYPE)
10945 return lookup_type_die (type_main_variant (type));
10946 else
10947 /* Vectors have the debugging information in the type,
10948 not the main variant. */
10949 return lookup_type_die (type);
10952 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10953 don't output a DW_TAG_typedef, since there isn't one in the
10954 user's program; just attach a DW_AT_name to the type.
10955 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10956 if the base type already has the same name. */
10957 if (name
10958 && ((TREE_CODE (name) != TYPE_DECL
10959 && (qualified_type == TYPE_MAIN_VARIANT (type)
10960 || (cv_quals == TYPE_UNQUALIFIED)))
10961 || (TREE_CODE (name) == TYPE_DECL
10962 && TREE_TYPE (name) == qualified_type
10963 && DECL_NAME (name))))
10965 if (TREE_CODE (name) == TYPE_DECL)
10966 /* Could just call add_name_and_src_coords_attributes here,
10967 but since this is a builtin type it doesn't have any
10968 useful source coordinates anyway. */
10969 name = DECL_NAME (name);
10970 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10972 /* This probably indicates a bug. */
10973 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10975 name = TYPE_IDENTIFIER (type);
10976 add_name_attribute (mod_type_die,
10977 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10980 if (qualified_type)
10981 equate_type_number_to_die (qualified_type, mod_type_die);
10983 if (item_type)
10984 /* We must do this after the equate_type_number_to_die call, in case
10985 this is a recursive type. This ensures that the modified_type_die
10986 recursion will terminate even if the type is recursive. Recursive
10987 types are possible in Ada. */
10988 sub_die = modified_type_die (item_type,
10989 TYPE_QUALS_NO_ADDR_SPACE (item_type),
10990 context_die);
10992 if (sub_die != NULL)
10993 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10995 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10996 if (TYPE_ARTIFICIAL (type))
10997 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10999 return mod_type_die;
11002 /* Generate DIEs for the generic parameters of T.
11003 T must be either a generic type or a generic function.
11004 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
11006 static void
11007 gen_generic_params_dies (tree t)
11009 tree parms, args;
11010 int parms_num, i;
11011 dw_die_ref die = NULL;
11012 int non_default;
11014 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
11015 return;
11017 if (TYPE_P (t))
11018 die = lookup_type_die (t);
11019 else if (DECL_P (t))
11020 die = lookup_decl_die (t);
11022 gcc_assert (die);
11024 parms = lang_hooks.get_innermost_generic_parms (t);
11025 if (!parms)
11026 /* T has no generic parameter. It means T is neither a generic type
11027 or function. End of story. */
11028 return;
11030 parms_num = TREE_VEC_LENGTH (parms);
11031 args = lang_hooks.get_innermost_generic_args (t);
11032 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
11033 non_default = int_cst_value (TREE_CHAIN (args));
11034 else
11035 non_default = TREE_VEC_LENGTH (args);
11036 for (i = 0; i < parms_num; i++)
11038 tree parm, arg, arg_pack_elems;
11039 dw_die_ref parm_die;
11041 parm = TREE_VEC_ELT (parms, i);
11042 arg = TREE_VEC_ELT (args, i);
11043 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
11044 gcc_assert (parm && TREE_VALUE (parm) && arg);
11046 if (parm && TREE_VALUE (parm) && arg)
11048 /* If PARM represents a template parameter pack,
11049 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
11050 by DW_TAG_template_*_parameter DIEs for the argument
11051 pack elements of ARG. Note that ARG would then be
11052 an argument pack. */
11053 if (arg_pack_elems)
11054 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
11055 arg_pack_elems,
11056 die);
11057 else
11058 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
11059 true /* emit name */, die);
11060 if (i >= non_default)
11061 add_AT_flag (parm_die, DW_AT_default_value, 1);
11066 /* Create and return a DIE for PARM which should be
11067 the representation of a generic type parameter.
11068 For instance, in the C++ front end, PARM would be a template parameter.
11069 ARG is the argument to PARM.
11070 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
11071 name of the PARM.
11072 PARENT_DIE is the parent DIE which the new created DIE should be added to,
11073 as a child node. */
11075 static dw_die_ref
11076 generic_parameter_die (tree parm, tree arg,
11077 bool emit_name_p,
11078 dw_die_ref parent_die)
11080 dw_die_ref tmpl_die = NULL;
11081 const char *name = NULL;
11083 if (!parm || !DECL_NAME (parm) || !arg)
11084 return NULL;
11086 /* We support non-type generic parameters and arguments,
11087 type generic parameters and arguments, as well as
11088 generic generic parameters (a.k.a. template template parameters in C++)
11089 and arguments. */
11090 if (TREE_CODE (parm) == PARM_DECL)
11091 /* PARM is a nontype generic parameter */
11092 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
11093 else if (TREE_CODE (parm) == TYPE_DECL)
11094 /* PARM is a type generic parameter. */
11095 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
11096 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11097 /* PARM is a generic generic parameter.
11098 Its DIE is a GNU extension. It shall have a
11099 DW_AT_name attribute to represent the name of the template template
11100 parameter, and a DW_AT_GNU_template_name attribute to represent the
11101 name of the template template argument. */
11102 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
11103 parent_die, parm);
11104 else
11105 gcc_unreachable ();
11107 if (tmpl_die)
11109 tree tmpl_type;
11111 /* If PARM is a generic parameter pack, it means we are
11112 emitting debug info for a template argument pack element.
11113 In other terms, ARG is a template argument pack element.
11114 In that case, we don't emit any DW_AT_name attribute for
11115 the die. */
11116 if (emit_name_p)
11118 name = IDENTIFIER_POINTER (DECL_NAME (parm));
11119 gcc_assert (name);
11120 add_AT_string (tmpl_die, DW_AT_name, name);
11123 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11125 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
11126 TMPL_DIE should have a child DW_AT_type attribute that is set
11127 to the type of the argument to PARM, which is ARG.
11128 If PARM is a type generic parameter, TMPL_DIE should have a
11129 child DW_AT_type that is set to ARG. */
11130 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
11131 add_type_attribute (tmpl_die, tmpl_type,
11132 (TREE_THIS_VOLATILE (tmpl_type)
11133 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11134 parent_die);
11136 else
11138 /* So TMPL_DIE is a DIE representing a
11139 a generic generic template parameter, a.k.a template template
11140 parameter in C++ and arg is a template. */
11142 /* The DW_AT_GNU_template_name attribute of the DIE must be set
11143 to the name of the argument. */
11144 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11145 if (name)
11146 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11149 if (TREE_CODE (parm) == PARM_DECL)
11150 /* So PARM is a non-type generic parameter.
11151 DWARF3 5.6.8 says we must set a DW_AT_const_value child
11152 attribute of TMPL_DIE which value represents the value
11153 of ARG.
11154 We must be careful here:
11155 The value of ARG might reference some function decls.
11156 We might currently be emitting debug info for a generic
11157 type and types are emitted before function decls, we don't
11158 know if the function decls referenced by ARG will actually be
11159 emitted after cgraph computations.
11160 So must defer the generation of the DW_AT_const_value to
11161 after cgraph is ready. */
11162 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11165 return tmpl_die;
11168 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
11169 PARM_PACK must be a template parameter pack. The returned DIE
11170 will be child DIE of PARENT_DIE. */
11172 static dw_die_ref
11173 template_parameter_pack_die (tree parm_pack,
11174 tree parm_pack_args,
11175 dw_die_ref parent_die)
11177 dw_die_ref die;
11178 int j;
11180 gcc_assert (parent_die && parm_pack);
11182 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11183 add_name_and_src_coords_attributes (die, parm_pack);
11184 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11185 generic_parameter_die (parm_pack,
11186 TREE_VEC_ELT (parm_pack_args, j),
11187 false /* Don't emit DW_AT_name */,
11188 die);
11189 return die;
11192 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11193 an enumerated type. */
11195 static inline int
11196 type_is_enum (const_tree type)
11198 return TREE_CODE (type) == ENUMERAL_TYPE;
11201 /* Return the DBX register number described by a given RTL node. */
11203 static unsigned int
11204 dbx_reg_number (const_rtx rtl)
11206 unsigned regno = REGNO (rtl);
11208 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11210 #ifdef LEAF_REG_REMAP
11211 if (crtl->uses_only_leaf_regs)
11213 int leaf_reg = LEAF_REG_REMAP (regno);
11214 if (leaf_reg != -1)
11215 regno = (unsigned) leaf_reg;
11217 #endif
11219 regno = DBX_REGISTER_NUMBER (regno);
11220 gcc_assert (regno != INVALID_REGNUM);
11221 return regno;
11224 /* Optionally add a DW_OP_piece term to a location description expression.
11225 DW_OP_piece is only added if the location description expression already
11226 doesn't end with DW_OP_piece. */
11228 static void
11229 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11231 dw_loc_descr_ref loc;
11233 if (*list_head != NULL)
11235 /* Find the end of the chain. */
11236 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11239 if (loc->dw_loc_opc != DW_OP_piece)
11240 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11244 /* Return a location descriptor that designates a machine register or
11245 zero if there is none. */
11247 static dw_loc_descr_ref
11248 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11250 rtx regs;
11252 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11253 return 0;
11255 /* We only use "frame base" when we're sure we're talking about the
11256 post-prologue local stack frame. We do this by *not* running
11257 register elimination until this point, and recognizing the special
11258 argument pointer and soft frame pointer rtx's.
11259 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
11260 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11261 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11263 dw_loc_descr_ref result = NULL;
11265 if (dwarf_version >= 4 || !dwarf_strict)
11267 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11268 initialized);
11269 if (result)
11270 add_loc_descr (&result,
11271 new_loc_descr (DW_OP_stack_value, 0, 0));
11273 return result;
11276 regs = targetm.dwarf_register_span (rtl);
11278 if (REG_NREGS (rtl) > 1 || regs)
11279 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11280 else
11282 unsigned int dbx_regnum = dbx_reg_number (rtl);
11283 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11284 return 0;
11285 return one_reg_loc_descriptor (dbx_regnum, initialized);
11289 /* Return a location descriptor that designates a machine register for
11290 a given hard register number. */
11292 static dw_loc_descr_ref
11293 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11295 dw_loc_descr_ref reg_loc_descr;
11297 if (regno <= 31)
11298 reg_loc_descr
11299 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11300 else
11301 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11303 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11304 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11306 return reg_loc_descr;
11309 /* Given an RTL of a register, return a location descriptor that
11310 designates a value that spans more than one register. */
11312 static dw_loc_descr_ref
11313 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11314 enum var_init_status initialized)
11316 int size, i;
11317 dw_loc_descr_ref loc_result = NULL;
11319 /* Simple, contiguous registers. */
11320 if (regs == NULL_RTX)
11322 unsigned reg = REGNO (rtl);
11323 int nregs;
11325 #ifdef LEAF_REG_REMAP
11326 if (crtl->uses_only_leaf_regs)
11328 int leaf_reg = LEAF_REG_REMAP (reg);
11329 if (leaf_reg != -1)
11330 reg = (unsigned) leaf_reg;
11332 #endif
11334 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11335 nregs = REG_NREGS (rtl);
11337 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11339 loc_result = NULL;
11340 while (nregs--)
11342 dw_loc_descr_ref t;
11344 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11345 VAR_INIT_STATUS_INITIALIZED);
11346 add_loc_descr (&loc_result, t);
11347 add_loc_descr_op_piece (&loc_result, size);
11348 ++reg;
11350 return loc_result;
11353 /* Now onto stupid register sets in non contiguous locations. */
11355 gcc_assert (GET_CODE (regs) == PARALLEL);
11357 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11358 loc_result = NULL;
11360 for (i = 0; i < XVECLEN (regs, 0); ++i)
11362 dw_loc_descr_ref t;
11364 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11365 VAR_INIT_STATUS_INITIALIZED);
11366 add_loc_descr (&loc_result, t);
11367 add_loc_descr_op_piece (&loc_result, size);
11370 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11371 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11372 return loc_result;
11375 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11377 /* Return a location descriptor that designates a constant i,
11378 as a compound operation from constant (i >> shift), constant shift
11379 and DW_OP_shl. */
11381 static dw_loc_descr_ref
11382 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11384 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11385 add_loc_descr (&ret, int_loc_descriptor (shift));
11386 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11387 return ret;
11390 /* Return a location descriptor that designates a constant. */
11392 static dw_loc_descr_ref
11393 int_loc_descriptor (HOST_WIDE_INT i)
11395 enum dwarf_location_atom op;
11397 /* Pick the smallest representation of a constant, rather than just
11398 defaulting to the LEB encoding. */
11399 if (i >= 0)
11401 int clz = clz_hwi (i);
11402 int ctz = ctz_hwi (i);
11403 if (i <= 31)
11404 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11405 else if (i <= 0xff)
11406 op = DW_OP_const1u;
11407 else if (i <= 0xffff)
11408 op = DW_OP_const2u;
11409 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11410 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11411 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11412 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11413 while DW_OP_const4u is 5 bytes. */
11414 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11415 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11416 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11417 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11418 while DW_OP_const4u is 5 bytes. */
11419 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11420 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11421 op = DW_OP_const4u;
11422 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11423 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11424 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11425 while DW_OP_constu of constant >= 0x100000000 takes at least
11426 6 bytes. */
11427 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11428 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11429 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11430 >= HOST_BITS_PER_WIDE_INT)
11431 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11432 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11433 while DW_OP_constu takes in this case at least 6 bytes. */
11434 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11435 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11436 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11437 && size_of_uleb128 (i) > 6)
11438 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11439 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11440 else
11441 op = DW_OP_constu;
11443 else
11445 if (i >= -0x80)
11446 op = DW_OP_const1s;
11447 else if (i >= -0x8000)
11448 op = DW_OP_const2s;
11449 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11451 if (size_of_int_loc_descriptor (i) < 5)
11453 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11454 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11455 return ret;
11457 op = DW_OP_const4s;
11459 else
11461 if (size_of_int_loc_descriptor (i)
11462 < (unsigned long) 1 + size_of_sleb128 (i))
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_consts;
11472 return new_loc_descr (op, i, 0);
11475 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11476 without actually allocating it. */
11478 static unsigned long
11479 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11481 return size_of_int_loc_descriptor (i >> shift)
11482 + size_of_int_loc_descriptor (shift)
11483 + 1;
11486 /* Return size_of_locs (int_loc_descriptor (i)) without
11487 actually allocating it. */
11489 static unsigned long
11490 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11492 unsigned long s;
11494 if (i >= 0)
11496 int clz, ctz;
11497 if (i <= 31)
11498 return 1;
11499 else if (i <= 0xff)
11500 return 2;
11501 else if (i <= 0xffff)
11502 return 3;
11503 clz = clz_hwi (i);
11504 ctz = ctz_hwi (i);
11505 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11506 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11507 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11508 - clz - 5);
11509 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11510 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11511 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11512 - clz - 8);
11513 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11514 return 5;
11515 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11516 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11517 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11518 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11519 - clz - 8);
11520 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11521 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11522 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11523 - clz - 16);
11524 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11525 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11526 && s > 6)
11527 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11528 - clz - 32);
11529 else
11530 return 1 + s;
11532 else
11534 if (i >= -0x80)
11535 return 2;
11536 else if (i >= -0x8000)
11537 return 3;
11538 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11540 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11542 s = size_of_int_loc_descriptor (-i) + 1;
11543 if (s < 5)
11544 return s;
11546 return 5;
11548 else
11550 unsigned long r = 1 + size_of_sleb128 (i);
11551 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11553 s = size_of_int_loc_descriptor (-i) + 1;
11554 if (s < r)
11555 return s;
11557 return r;
11562 /* Return loc description representing "address" of integer value.
11563 This can appear only as toplevel expression. */
11565 static dw_loc_descr_ref
11566 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11568 int litsize;
11569 dw_loc_descr_ref loc_result = NULL;
11571 if (!(dwarf_version >= 4 || !dwarf_strict))
11572 return NULL;
11574 litsize = size_of_int_loc_descriptor (i);
11575 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11576 is more compact. For DW_OP_stack_value we need:
11577 litsize + 1 (DW_OP_stack_value)
11578 and for DW_OP_implicit_value:
11579 1 (DW_OP_implicit_value) + 1 (length) + size. */
11580 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11582 loc_result = int_loc_descriptor (i);
11583 add_loc_descr (&loc_result,
11584 new_loc_descr (DW_OP_stack_value, 0, 0));
11585 return loc_result;
11588 loc_result = new_loc_descr (DW_OP_implicit_value,
11589 size, 0);
11590 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11591 loc_result->dw_loc_oprnd2.v.val_int = i;
11592 return loc_result;
11595 /* Return a location descriptor that designates a base+offset location. */
11597 static dw_loc_descr_ref
11598 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11599 enum var_init_status initialized)
11601 unsigned int regno;
11602 dw_loc_descr_ref result;
11603 dw_fde_ref fde = cfun->fde;
11605 /* We only use "frame base" when we're sure we're talking about the
11606 post-prologue local stack frame. We do this by *not* running
11607 register elimination until this point, and recognizing the special
11608 argument pointer and soft frame pointer rtx's. */
11609 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11611 rtx elim = (ira_use_lra_p
11612 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11613 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11615 if (elim != reg)
11617 if (GET_CODE (elim) == PLUS)
11619 offset += INTVAL (XEXP (elim, 1));
11620 elim = XEXP (elim, 0);
11622 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11623 && (elim == hard_frame_pointer_rtx
11624 || elim == stack_pointer_rtx))
11625 || elim == (frame_pointer_needed
11626 ? hard_frame_pointer_rtx
11627 : stack_pointer_rtx));
11629 /* If drap register is used to align stack, use frame
11630 pointer + offset to access stack variables. If stack
11631 is aligned without drap, use stack pointer + offset to
11632 access stack variables. */
11633 if (crtl->stack_realign_tried
11634 && reg == frame_pointer_rtx)
11636 int base_reg
11637 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11638 ? HARD_FRAME_POINTER_REGNUM
11639 : REGNO (elim));
11640 return new_reg_loc_descr (base_reg, offset);
11643 gcc_assert (frame_pointer_fb_offset_valid);
11644 offset += frame_pointer_fb_offset;
11645 return new_loc_descr (DW_OP_fbreg, offset, 0);
11649 regno = REGNO (reg);
11650 #ifdef LEAF_REG_REMAP
11651 if (crtl->uses_only_leaf_regs)
11653 int leaf_reg = LEAF_REG_REMAP (regno);
11654 if (leaf_reg != -1)
11655 regno = (unsigned) leaf_reg;
11657 #endif
11658 regno = DWARF_FRAME_REGNUM (regno);
11660 if (!optimize && fde
11661 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11663 /* Use cfa+offset to represent the location of arguments passed
11664 on the stack when drap is used to align stack.
11665 Only do this when not optimizing, for optimized code var-tracking
11666 is supposed to track where the arguments live and the register
11667 used as vdrap or drap in some spot might be used for something
11668 else in other part of the routine. */
11669 return new_loc_descr (DW_OP_fbreg, offset, 0);
11672 if (regno <= 31)
11673 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11674 offset, 0);
11675 else
11676 result = new_loc_descr (DW_OP_bregx, regno, offset);
11678 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11679 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11681 return result;
11684 /* Return true if this RTL expression describes a base+offset calculation. */
11686 static inline int
11687 is_based_loc (const_rtx rtl)
11689 return (GET_CODE (rtl) == PLUS
11690 && ((REG_P (XEXP (rtl, 0))
11691 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11692 && CONST_INT_P (XEXP (rtl, 1)))));
11695 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11696 failed. */
11698 static dw_loc_descr_ref
11699 tls_mem_loc_descriptor (rtx mem)
11701 tree base;
11702 dw_loc_descr_ref loc_result;
11704 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11705 return NULL;
11707 base = get_base_address (MEM_EXPR (mem));
11708 if (base == NULL
11709 || TREE_CODE (base) != VAR_DECL
11710 || !DECL_THREAD_LOCAL_P (base))
11711 return NULL;
11713 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
11714 if (loc_result == NULL)
11715 return NULL;
11717 if (MEM_OFFSET (mem))
11718 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11720 return loc_result;
11723 /* Output debug info about reason why we failed to expand expression as dwarf
11724 expression. */
11726 static void
11727 expansion_failed (tree expr, rtx rtl, char const *reason)
11729 if (dump_file && (dump_flags & TDF_DETAILS))
11731 fprintf (dump_file, "Failed to expand as dwarf: ");
11732 if (expr)
11733 print_generic_expr (dump_file, expr, dump_flags);
11734 if (rtl)
11736 fprintf (dump_file, "\n");
11737 print_rtl (dump_file, rtl);
11739 fprintf (dump_file, "\nReason: %s\n", reason);
11743 /* Helper function for const_ok_for_output. */
11745 static bool
11746 const_ok_for_output_1 (rtx rtl)
11748 if (GET_CODE (rtl) == UNSPEC)
11750 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11751 we can't express it in the debug info. */
11752 /* Don't complain about TLS UNSPECs, those are just too hard to
11753 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11754 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11755 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11756 if (flag_checking
11757 && (XVECLEN (rtl, 0) == 0
11758 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11759 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
11760 inform (current_function_decl
11761 ? DECL_SOURCE_LOCATION (current_function_decl)
11762 : UNKNOWN_LOCATION,
11763 #if NUM_UNSPEC_VALUES > 0
11764 "non-delegitimized UNSPEC %s (%d) found in variable location",
11765 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11766 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11767 XINT (rtl, 1));
11768 #else
11769 "non-delegitimized UNSPEC %d found in variable location",
11770 XINT (rtl, 1));
11771 #endif
11772 expansion_failed (NULL_TREE, rtl,
11773 "UNSPEC hasn't been delegitimized.\n");
11774 return false;
11777 if (targetm.const_not_ok_for_debug_p (rtl))
11779 expansion_failed (NULL_TREE, rtl,
11780 "Expression rejected for debug by the backend.\n");
11781 return false;
11784 /* FIXME: Refer to PR60655. It is possible for simplification
11785 of rtl expressions in var tracking to produce such expressions.
11786 We should really identify / validate expressions
11787 enclosed in CONST that can be handled by assemblers on various
11788 targets and only handle legitimate cases here. */
11789 if (GET_CODE (rtl) != SYMBOL_REF)
11791 if (GET_CODE (rtl) == NOT)
11792 return false;
11793 return true;
11796 if (CONSTANT_POOL_ADDRESS_P (rtl))
11798 bool marked;
11799 get_pool_constant_mark (rtl, &marked);
11800 /* If all references to this pool constant were optimized away,
11801 it was not output and thus we can't represent it. */
11802 if (!marked)
11804 expansion_failed (NULL_TREE, rtl,
11805 "Constant was removed from constant pool.\n");
11806 return false;
11810 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11811 return false;
11813 /* Avoid references to external symbols in debug info, on several targets
11814 the linker might even refuse to link when linking a shared library,
11815 and in many other cases the relocations for .debug_info/.debug_loc are
11816 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11817 to be defined within the same shared library or executable are fine. */
11818 if (SYMBOL_REF_EXTERNAL_P (rtl))
11820 tree decl = SYMBOL_REF_DECL (rtl);
11822 if (decl == NULL || !targetm.binds_local_p (decl))
11824 expansion_failed (NULL_TREE, rtl,
11825 "Symbol not defined in current TU.\n");
11826 return false;
11830 return true;
11833 /* Return true if constant RTL can be emitted in DW_OP_addr or
11834 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11835 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11837 static bool
11838 const_ok_for_output (rtx rtl)
11840 if (GET_CODE (rtl) == SYMBOL_REF)
11841 return const_ok_for_output_1 (rtl);
11843 if (GET_CODE (rtl) == CONST)
11845 subrtx_var_iterator::array_type array;
11846 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11847 if (!const_ok_for_output_1 (*iter))
11848 return false;
11849 return true;
11852 return true;
11855 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11856 if possible, NULL otherwise. */
11858 static dw_die_ref
11859 base_type_for_mode (machine_mode mode, bool unsignedp)
11861 dw_die_ref type_die;
11862 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11864 if (type == NULL)
11865 return NULL;
11866 switch (TREE_CODE (type))
11868 case INTEGER_TYPE:
11869 case REAL_TYPE:
11870 break;
11871 default:
11872 return NULL;
11874 type_die = lookup_type_die (type);
11875 if (!type_die)
11876 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11877 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11878 return NULL;
11879 return type_die;
11882 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11883 type matching MODE, or, if MODE is narrower than or as wide as
11884 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11885 possible. */
11887 static dw_loc_descr_ref
11888 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
11890 machine_mode outer_mode = mode;
11891 dw_die_ref type_die;
11892 dw_loc_descr_ref cvt;
11894 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11896 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11897 return op;
11899 type_die = base_type_for_mode (outer_mode, 1);
11900 if (type_die == NULL)
11901 return NULL;
11902 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11903 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11904 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11905 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11906 add_loc_descr (&op, cvt);
11907 return op;
11910 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11912 static dw_loc_descr_ref
11913 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11914 dw_loc_descr_ref op1)
11916 dw_loc_descr_ref ret = op0;
11917 add_loc_descr (&ret, op1);
11918 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11919 if (STORE_FLAG_VALUE != 1)
11921 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11922 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11924 return ret;
11927 /* Return location descriptor for signed comparison OP RTL. */
11929 static dw_loc_descr_ref
11930 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11931 machine_mode mem_mode)
11933 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11934 dw_loc_descr_ref op0, op1;
11935 int shift;
11937 if (op_mode == VOIDmode)
11938 op_mode = GET_MODE (XEXP (rtl, 1));
11939 if (op_mode == VOIDmode)
11940 return NULL;
11942 if (dwarf_strict
11943 && (GET_MODE_CLASS (op_mode) != MODE_INT
11944 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11945 return NULL;
11947 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11948 VAR_INIT_STATUS_INITIALIZED);
11949 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11950 VAR_INIT_STATUS_INITIALIZED);
11952 if (op0 == NULL || op1 == NULL)
11953 return NULL;
11955 if (GET_MODE_CLASS (op_mode) != MODE_INT
11956 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11957 return compare_loc_descriptor (op, op0, op1);
11959 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11961 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11962 dw_loc_descr_ref cvt;
11964 if (type_die == NULL)
11965 return NULL;
11966 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11967 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11968 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11969 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11970 add_loc_descr (&op0, cvt);
11971 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11972 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11973 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11974 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11975 add_loc_descr (&op1, cvt);
11976 return compare_loc_descriptor (op, op0, op1);
11979 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11980 /* For eq/ne, if the operands are known to be zero-extended,
11981 there is no need to do the fancy shifting up. */
11982 if (op == DW_OP_eq || op == DW_OP_ne)
11984 dw_loc_descr_ref last0, last1;
11985 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11987 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11989 /* deref_size zero extends, and for constants we can check
11990 whether they are zero extended or not. */
11991 if (((last0->dw_loc_opc == DW_OP_deref_size
11992 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11993 || (CONST_INT_P (XEXP (rtl, 0))
11994 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11995 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11996 && ((last1->dw_loc_opc == DW_OP_deref_size
11997 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11998 || (CONST_INT_P (XEXP (rtl, 1))
11999 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
12000 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
12001 return compare_loc_descriptor (op, op0, op1);
12003 /* EQ/NE comparison against constant in narrower type than
12004 DWARF2_ADDR_SIZE can be performed either as
12005 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
12006 DW_OP_{eq,ne}
12008 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
12009 DW_OP_{eq,ne}. Pick whatever is shorter. */
12010 if (CONST_INT_P (XEXP (rtl, 1))
12011 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
12012 && (size_of_int_loc_descriptor (shift) + 1
12013 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
12014 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
12015 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12016 & GET_MODE_MASK (op_mode))))
12018 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
12019 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12020 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12021 & GET_MODE_MASK (op_mode));
12022 return compare_loc_descriptor (op, op0, op1);
12025 add_loc_descr (&op0, int_loc_descriptor (shift));
12026 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12027 if (CONST_INT_P (XEXP (rtl, 1)))
12028 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
12029 else
12031 add_loc_descr (&op1, int_loc_descriptor (shift));
12032 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12034 return compare_loc_descriptor (op, op0, op1);
12037 /* Return location descriptor for unsigned comparison OP RTL. */
12039 static dw_loc_descr_ref
12040 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12041 machine_mode mem_mode)
12043 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12044 dw_loc_descr_ref op0, op1;
12046 if (op_mode == VOIDmode)
12047 op_mode = GET_MODE (XEXP (rtl, 1));
12048 if (op_mode == VOIDmode)
12049 return NULL;
12050 if (GET_MODE_CLASS (op_mode) != MODE_INT)
12051 return NULL;
12053 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12054 return NULL;
12056 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12057 VAR_INIT_STATUS_INITIALIZED);
12058 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12059 VAR_INIT_STATUS_INITIALIZED);
12061 if (op0 == NULL || op1 == NULL)
12062 return NULL;
12064 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
12066 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
12067 dw_loc_descr_ref last0, last1;
12068 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12070 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12072 if (CONST_INT_P (XEXP (rtl, 0)))
12073 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
12074 /* deref_size zero extends, so no need to mask it again. */
12075 else if (last0->dw_loc_opc != DW_OP_deref_size
12076 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12078 add_loc_descr (&op0, int_loc_descriptor (mask));
12079 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12081 if (CONST_INT_P (XEXP (rtl, 1)))
12082 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
12083 /* deref_size zero extends, so no need to mask it again. */
12084 else if (last1->dw_loc_opc != DW_OP_deref_size
12085 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12087 add_loc_descr (&op1, int_loc_descriptor (mask));
12088 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12091 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12093 HOST_WIDE_INT bias = 1;
12094 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12095 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12096 if (CONST_INT_P (XEXP (rtl, 1)))
12097 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
12098 + INTVAL (XEXP (rtl, 1)));
12099 else
12100 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
12101 bias, 0));
12103 return compare_loc_descriptor (op, op0, op1);
12106 /* Return location descriptor for {U,S}{MIN,MAX}. */
12108 static dw_loc_descr_ref
12109 minmax_loc_descriptor (rtx rtl, machine_mode mode,
12110 machine_mode mem_mode)
12112 enum dwarf_location_atom op;
12113 dw_loc_descr_ref op0, op1, ret;
12114 dw_loc_descr_ref bra_node, drop_node;
12116 if (dwarf_strict
12117 && (GET_MODE_CLASS (mode) != MODE_INT
12118 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
12119 return NULL;
12121 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12122 VAR_INIT_STATUS_INITIALIZED);
12123 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12124 VAR_INIT_STATUS_INITIALIZED);
12126 if (op0 == NULL || op1 == NULL)
12127 return NULL;
12129 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
12130 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12131 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12132 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12134 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12136 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12137 add_loc_descr (&op0, int_loc_descriptor (mask));
12138 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12139 add_loc_descr (&op1, int_loc_descriptor (mask));
12140 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12142 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12144 HOST_WIDE_INT bias = 1;
12145 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12146 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12147 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12150 else if (GET_MODE_CLASS (mode) == MODE_INT
12151 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12153 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12154 add_loc_descr (&op0, int_loc_descriptor (shift));
12155 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12156 add_loc_descr (&op1, int_loc_descriptor (shift));
12157 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12159 else if (GET_MODE_CLASS (mode) == MODE_INT
12160 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12162 dw_die_ref type_die = base_type_for_mode (mode, 0);
12163 dw_loc_descr_ref cvt;
12164 if (type_die == NULL)
12165 return NULL;
12166 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12167 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12168 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12169 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12170 add_loc_descr (&op0, cvt);
12171 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12172 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12173 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12174 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12175 add_loc_descr (&op1, cvt);
12178 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12179 op = DW_OP_lt;
12180 else
12181 op = DW_OP_gt;
12182 ret = op0;
12183 add_loc_descr (&ret, op1);
12184 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12185 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12186 add_loc_descr (&ret, bra_node);
12187 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12188 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12189 add_loc_descr (&ret, drop_node);
12190 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12191 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12192 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12193 && GET_MODE_CLASS (mode) == MODE_INT
12194 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12195 ret = convert_descriptor_to_mode (mode, ret);
12196 return ret;
12199 /* Helper function for mem_loc_descriptor. Perform OP binary op,
12200 but after converting arguments to type_die, afterwards
12201 convert back to unsigned. */
12203 static dw_loc_descr_ref
12204 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12205 machine_mode mode, machine_mode mem_mode)
12207 dw_loc_descr_ref cvt, op0, op1;
12209 if (type_die == NULL)
12210 return NULL;
12211 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12212 VAR_INIT_STATUS_INITIALIZED);
12213 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12214 VAR_INIT_STATUS_INITIALIZED);
12215 if (op0 == NULL || op1 == NULL)
12216 return NULL;
12217 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12218 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12219 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12220 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12221 add_loc_descr (&op0, cvt);
12222 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12223 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12224 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12225 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12226 add_loc_descr (&op1, cvt);
12227 add_loc_descr (&op0, op1);
12228 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12229 return convert_descriptor_to_mode (mode, op0);
12232 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12233 const0 is DW_OP_lit0 or corresponding typed constant,
12234 const1 is DW_OP_lit1 or corresponding typed constant
12235 and constMSB is constant with just the MSB bit set
12236 for the mode):
12237 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12238 L1: const0 DW_OP_swap
12239 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12240 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12241 L3: DW_OP_drop
12242 L4: DW_OP_nop
12244 CTZ is similar:
12245 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12246 L1: const0 DW_OP_swap
12247 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12248 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12249 L3: DW_OP_drop
12250 L4: DW_OP_nop
12252 FFS is similar:
12253 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12254 L1: const1 DW_OP_swap
12255 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12256 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12257 L3: DW_OP_drop
12258 L4: DW_OP_nop */
12260 static dw_loc_descr_ref
12261 clz_loc_descriptor (rtx rtl, machine_mode mode,
12262 machine_mode mem_mode)
12264 dw_loc_descr_ref op0, ret, tmp;
12265 HOST_WIDE_INT valv;
12266 dw_loc_descr_ref l1jump, l1label;
12267 dw_loc_descr_ref l2jump, l2label;
12268 dw_loc_descr_ref l3jump, l3label;
12269 dw_loc_descr_ref l4jump, l4label;
12270 rtx msb;
12272 if (GET_MODE_CLASS (mode) != MODE_INT
12273 || GET_MODE (XEXP (rtl, 0)) != mode)
12274 return NULL;
12276 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12277 VAR_INIT_STATUS_INITIALIZED);
12278 if (op0 == NULL)
12279 return NULL;
12280 ret = op0;
12281 if (GET_CODE (rtl) == CLZ)
12283 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12284 valv = GET_MODE_BITSIZE (mode);
12286 else if (GET_CODE (rtl) == FFS)
12287 valv = 0;
12288 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12289 valv = GET_MODE_BITSIZE (mode);
12290 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12291 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12292 add_loc_descr (&ret, l1jump);
12293 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12294 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12295 VAR_INIT_STATUS_INITIALIZED);
12296 if (tmp == NULL)
12297 return NULL;
12298 add_loc_descr (&ret, tmp);
12299 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12300 add_loc_descr (&ret, l4jump);
12301 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12302 ? const1_rtx : const0_rtx,
12303 mode, mem_mode,
12304 VAR_INIT_STATUS_INITIALIZED);
12305 if (l1label == NULL)
12306 return NULL;
12307 add_loc_descr (&ret, l1label);
12308 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12309 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12310 add_loc_descr (&ret, l2label);
12311 if (GET_CODE (rtl) != CLZ)
12312 msb = const1_rtx;
12313 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12314 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12315 << (GET_MODE_BITSIZE (mode) - 1));
12316 else
12317 msb = immed_wide_int_const
12318 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12319 GET_MODE_PRECISION (mode)), mode);
12320 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12321 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12322 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12323 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12324 else
12325 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12326 VAR_INIT_STATUS_INITIALIZED);
12327 if (tmp == NULL)
12328 return NULL;
12329 add_loc_descr (&ret, tmp);
12330 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12331 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12332 add_loc_descr (&ret, l3jump);
12333 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12334 VAR_INIT_STATUS_INITIALIZED);
12335 if (tmp == NULL)
12336 return NULL;
12337 add_loc_descr (&ret, tmp);
12338 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12339 ? DW_OP_shl : DW_OP_shr, 0, 0));
12340 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12341 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12342 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12343 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12344 add_loc_descr (&ret, l2jump);
12345 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12346 add_loc_descr (&ret, l3label);
12347 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12348 add_loc_descr (&ret, l4label);
12349 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12350 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12351 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12352 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12353 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12354 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12355 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12356 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12357 return ret;
12360 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12361 const1 is DW_OP_lit1 or corresponding typed constant):
12362 const0 DW_OP_swap
12363 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12364 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12365 L2: DW_OP_drop
12367 PARITY is similar:
12368 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12369 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12370 L2: DW_OP_drop */
12372 static dw_loc_descr_ref
12373 popcount_loc_descriptor (rtx rtl, machine_mode mode,
12374 machine_mode mem_mode)
12376 dw_loc_descr_ref op0, ret, tmp;
12377 dw_loc_descr_ref l1jump, l1label;
12378 dw_loc_descr_ref l2jump, l2label;
12380 if (GET_MODE_CLASS (mode) != MODE_INT
12381 || GET_MODE (XEXP (rtl, 0)) != mode)
12382 return NULL;
12384 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12385 VAR_INIT_STATUS_INITIALIZED);
12386 if (op0 == NULL)
12387 return NULL;
12388 ret = op0;
12389 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12390 VAR_INIT_STATUS_INITIALIZED);
12391 if (tmp == NULL)
12392 return NULL;
12393 add_loc_descr (&ret, tmp);
12394 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12395 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12396 add_loc_descr (&ret, l1label);
12397 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12398 add_loc_descr (&ret, l2jump);
12399 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12400 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12401 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12402 VAR_INIT_STATUS_INITIALIZED);
12403 if (tmp == NULL)
12404 return NULL;
12405 add_loc_descr (&ret, tmp);
12406 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12407 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12408 ? DW_OP_plus : DW_OP_xor, 0, 0));
12409 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12410 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12411 VAR_INIT_STATUS_INITIALIZED);
12412 add_loc_descr (&ret, tmp);
12413 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12414 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12415 add_loc_descr (&ret, l1jump);
12416 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12417 add_loc_descr (&ret, l2label);
12418 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12419 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12420 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12421 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12422 return ret;
12425 /* BSWAP (constS is initial shift count, either 56 or 24):
12426 constS const0
12427 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12428 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12429 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12430 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12431 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12433 static dw_loc_descr_ref
12434 bswap_loc_descriptor (rtx rtl, machine_mode mode,
12435 machine_mode mem_mode)
12437 dw_loc_descr_ref op0, ret, tmp;
12438 dw_loc_descr_ref l1jump, l1label;
12439 dw_loc_descr_ref l2jump, l2label;
12441 if (GET_MODE_CLASS (mode) != MODE_INT
12442 || BITS_PER_UNIT != 8
12443 || (GET_MODE_BITSIZE (mode) != 32
12444 && GET_MODE_BITSIZE (mode) != 64))
12445 return NULL;
12447 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12448 VAR_INIT_STATUS_INITIALIZED);
12449 if (op0 == NULL)
12450 return NULL;
12452 ret = op0;
12453 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12454 mode, mem_mode,
12455 VAR_INIT_STATUS_INITIALIZED);
12456 if (tmp == NULL)
12457 return NULL;
12458 add_loc_descr (&ret, tmp);
12459 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12460 VAR_INIT_STATUS_INITIALIZED);
12461 if (tmp == NULL)
12462 return NULL;
12463 add_loc_descr (&ret, tmp);
12464 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12465 add_loc_descr (&ret, l1label);
12466 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12467 mode, mem_mode,
12468 VAR_INIT_STATUS_INITIALIZED);
12469 add_loc_descr (&ret, tmp);
12470 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12471 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12472 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12473 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12474 VAR_INIT_STATUS_INITIALIZED);
12475 if (tmp == NULL)
12476 return NULL;
12477 add_loc_descr (&ret, tmp);
12478 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12479 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12480 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12481 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12482 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12483 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12484 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12485 VAR_INIT_STATUS_INITIALIZED);
12486 add_loc_descr (&ret, tmp);
12487 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12488 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12489 add_loc_descr (&ret, l2jump);
12490 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12491 VAR_INIT_STATUS_INITIALIZED);
12492 add_loc_descr (&ret, tmp);
12493 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12494 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12495 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12496 add_loc_descr (&ret, l1jump);
12497 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12498 add_loc_descr (&ret, l2label);
12499 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12500 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12501 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12502 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12503 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12504 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12505 return ret;
12508 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12509 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12510 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12511 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12513 ROTATERT is similar:
12514 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12515 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12516 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12518 static dw_loc_descr_ref
12519 rotate_loc_descriptor (rtx rtl, machine_mode mode,
12520 machine_mode mem_mode)
12522 rtx rtlop1 = XEXP (rtl, 1);
12523 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12524 int i;
12526 if (GET_MODE_CLASS (mode) != MODE_INT)
12527 return NULL;
12529 if (GET_MODE (rtlop1) != VOIDmode
12530 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12531 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12532 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12533 VAR_INIT_STATUS_INITIALIZED);
12534 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12535 VAR_INIT_STATUS_INITIALIZED);
12536 if (op0 == NULL || op1 == NULL)
12537 return NULL;
12538 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12539 for (i = 0; i < 2; i++)
12541 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12542 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12543 mode, mem_mode,
12544 VAR_INIT_STATUS_INITIALIZED);
12545 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12546 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12547 ? DW_OP_const4u
12548 : HOST_BITS_PER_WIDE_INT == 64
12549 ? DW_OP_const8u : DW_OP_constu,
12550 GET_MODE_MASK (mode), 0);
12551 else
12552 mask[i] = NULL;
12553 if (mask[i] == NULL)
12554 return NULL;
12555 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12557 ret = op0;
12558 add_loc_descr (&ret, op1);
12559 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12560 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12561 if (GET_CODE (rtl) == ROTATERT)
12563 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12564 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12565 GET_MODE_BITSIZE (mode), 0));
12567 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12568 if (mask[0] != NULL)
12569 add_loc_descr (&ret, mask[0]);
12570 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12571 if (mask[1] != NULL)
12573 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12574 add_loc_descr (&ret, mask[1]);
12575 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12577 if (GET_CODE (rtl) == ROTATE)
12579 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12580 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12581 GET_MODE_BITSIZE (mode), 0));
12583 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12584 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12585 return ret;
12588 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12589 for DEBUG_PARAMETER_REF RTL. */
12591 static dw_loc_descr_ref
12592 parameter_ref_descriptor (rtx rtl)
12594 dw_loc_descr_ref ret;
12595 dw_die_ref ref;
12597 if (dwarf_strict)
12598 return NULL;
12599 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12600 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12601 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12602 if (ref)
12604 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12605 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12606 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12608 else
12610 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12611 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12613 return ret;
12616 /* The following routine converts the RTL for a variable or parameter
12617 (resident in memory) into an equivalent Dwarf representation of a
12618 mechanism for getting the address of that same variable onto the top of a
12619 hypothetical "address evaluation" stack.
12621 When creating memory location descriptors, we are effectively transforming
12622 the RTL for a memory-resident object into its Dwarf postfix expression
12623 equivalent. This routine recursively descends an RTL tree, turning
12624 it into Dwarf postfix code as it goes.
12626 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12628 MEM_MODE is the mode of the memory reference, needed to handle some
12629 autoincrement addressing modes.
12631 Return 0 if we can't represent the location. */
12633 dw_loc_descr_ref
12634 mem_loc_descriptor (rtx rtl, machine_mode mode,
12635 machine_mode mem_mode,
12636 enum var_init_status initialized)
12638 dw_loc_descr_ref mem_loc_result = NULL;
12639 enum dwarf_location_atom op;
12640 dw_loc_descr_ref op0, op1;
12641 rtx inner = NULL_RTX;
12643 if (mode == VOIDmode)
12644 mode = GET_MODE (rtl);
12646 /* Note that for a dynamically sized array, the location we will generate a
12647 description of here will be the lowest numbered location which is
12648 actually within the array. That's *not* necessarily the same as the
12649 zeroth element of the array. */
12651 rtl = targetm.delegitimize_address (rtl);
12653 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12654 return NULL;
12656 switch (GET_CODE (rtl))
12658 case POST_INC:
12659 case POST_DEC:
12660 case POST_MODIFY:
12661 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12663 case SUBREG:
12664 /* The case of a subreg may arise when we have a local (register)
12665 variable or a formal (register) parameter which doesn't quite fill
12666 up an entire register. For now, just assume that it is
12667 legitimate to make the Dwarf info refer to the whole register which
12668 contains the given subreg. */
12669 if (!subreg_lowpart_p (rtl))
12670 break;
12671 inner = SUBREG_REG (rtl);
12672 case TRUNCATE:
12673 if (inner == NULL_RTX)
12674 inner = XEXP (rtl, 0);
12675 if (GET_MODE_CLASS (mode) == MODE_INT
12676 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12677 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12678 #ifdef POINTERS_EXTEND_UNSIGNED
12679 || (mode == Pmode && mem_mode != VOIDmode)
12680 #endif
12682 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12684 mem_loc_result = mem_loc_descriptor (inner,
12685 GET_MODE (inner),
12686 mem_mode, initialized);
12687 break;
12689 if (dwarf_strict)
12690 break;
12691 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12692 break;
12693 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12694 && (GET_MODE_CLASS (mode) != MODE_INT
12695 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12696 break;
12697 else
12699 dw_die_ref type_die;
12700 dw_loc_descr_ref cvt;
12702 mem_loc_result = mem_loc_descriptor (inner,
12703 GET_MODE (inner),
12704 mem_mode, initialized);
12705 if (mem_loc_result == NULL)
12706 break;
12707 type_die = base_type_for_mode (mode,
12708 GET_MODE_CLASS (mode) == MODE_INT);
12709 if (type_die == NULL)
12711 mem_loc_result = NULL;
12712 break;
12714 if (GET_MODE_SIZE (mode)
12715 != GET_MODE_SIZE (GET_MODE (inner)))
12716 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12717 else
12718 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12719 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12720 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12721 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12722 add_loc_descr (&mem_loc_result, cvt);
12724 break;
12726 case REG:
12727 if (GET_MODE_CLASS (mode) != MODE_INT
12728 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12729 && rtl != arg_pointer_rtx
12730 && rtl != frame_pointer_rtx
12731 #ifdef POINTERS_EXTEND_UNSIGNED
12732 && (mode != Pmode || mem_mode == VOIDmode)
12733 #endif
12736 dw_die_ref type_die;
12737 unsigned int dbx_regnum;
12739 if (dwarf_strict)
12740 break;
12741 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12742 break;
12743 type_die = base_type_for_mode (mode,
12744 GET_MODE_CLASS (mode) == MODE_INT);
12745 if (type_die == NULL)
12746 break;
12748 dbx_regnum = dbx_reg_number (rtl);
12749 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12750 break;
12751 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12752 dbx_regnum, 0);
12753 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12754 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12755 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12756 break;
12758 /* Whenever a register number forms a part of the description of the
12759 method for calculating the (dynamic) address of a memory resident
12760 object, DWARF rules require the register number be referred to as
12761 a "base register". This distinction is not based in any way upon
12762 what category of register the hardware believes the given register
12763 belongs to. This is strictly DWARF terminology we're dealing with
12764 here. Note that in cases where the location of a memory-resident
12765 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12766 OP_CONST (0)) the actual DWARF location descriptor that we generate
12767 may just be OP_BASEREG (basereg). This may look deceptively like
12768 the object in question was allocated to a register (rather than in
12769 memory) so DWARF consumers need to be aware of the subtle
12770 distinction between OP_REG and OP_BASEREG. */
12771 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12772 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12773 else if (stack_realign_drap
12774 && crtl->drap_reg
12775 && crtl->args.internal_arg_pointer == rtl
12776 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12778 /* If RTL is internal_arg_pointer, which has been optimized
12779 out, use DRAP instead. */
12780 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12781 VAR_INIT_STATUS_INITIALIZED);
12783 break;
12785 case SIGN_EXTEND:
12786 case ZERO_EXTEND:
12787 if (GET_MODE_CLASS (mode) != MODE_INT)
12788 break;
12789 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12790 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12791 if (op0 == 0)
12792 break;
12793 else if (GET_CODE (rtl) == ZERO_EXTEND
12794 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12795 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12796 < HOST_BITS_PER_WIDE_INT
12797 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12798 to expand zero extend as two shifts instead of
12799 masking. */
12800 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12802 machine_mode imode = GET_MODE (XEXP (rtl, 0));
12803 mem_loc_result = op0;
12804 add_loc_descr (&mem_loc_result,
12805 int_loc_descriptor (GET_MODE_MASK (imode)));
12806 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12808 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12810 int shift = DWARF2_ADDR_SIZE
12811 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12812 shift *= BITS_PER_UNIT;
12813 if (GET_CODE (rtl) == SIGN_EXTEND)
12814 op = DW_OP_shra;
12815 else
12816 op = DW_OP_shr;
12817 mem_loc_result = op0;
12818 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12819 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12820 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12821 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12823 else if (!dwarf_strict)
12825 dw_die_ref type_die1, type_die2;
12826 dw_loc_descr_ref cvt;
12828 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12829 GET_CODE (rtl) == ZERO_EXTEND);
12830 if (type_die1 == NULL)
12831 break;
12832 type_die2 = base_type_for_mode (mode, 1);
12833 if (type_die2 == NULL)
12834 break;
12835 mem_loc_result = op0;
12836 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12837 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12838 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12839 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12840 add_loc_descr (&mem_loc_result, cvt);
12841 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12842 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12843 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12844 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12845 add_loc_descr (&mem_loc_result, cvt);
12847 break;
12849 case MEM:
12851 rtx new_rtl = avoid_constant_pool_reference (rtl);
12852 if (new_rtl != rtl)
12854 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12855 initialized);
12856 if (mem_loc_result != NULL)
12857 return mem_loc_result;
12860 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12861 get_address_mode (rtl), mode,
12862 VAR_INIT_STATUS_INITIALIZED);
12863 if (mem_loc_result == NULL)
12864 mem_loc_result = tls_mem_loc_descriptor (rtl);
12865 if (mem_loc_result != NULL)
12867 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12868 || GET_MODE_CLASS (mode) != MODE_INT)
12870 dw_die_ref type_die;
12871 dw_loc_descr_ref deref;
12873 if (dwarf_strict)
12874 return NULL;
12875 type_die
12876 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12877 if (type_die == NULL)
12878 return NULL;
12879 deref = new_loc_descr (DW_OP_GNU_deref_type,
12880 GET_MODE_SIZE (mode), 0);
12881 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12882 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12883 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12884 add_loc_descr (&mem_loc_result, deref);
12886 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12887 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12888 else
12889 add_loc_descr (&mem_loc_result,
12890 new_loc_descr (DW_OP_deref_size,
12891 GET_MODE_SIZE (mode), 0));
12893 break;
12895 case LO_SUM:
12896 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12898 case LABEL_REF:
12899 /* Some ports can transform a symbol ref into a label ref, because
12900 the symbol ref is too far away and has to be dumped into a constant
12901 pool. */
12902 case CONST:
12903 case SYMBOL_REF:
12904 if ((GET_MODE_CLASS (mode) != MODE_INT
12905 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12906 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12907 #ifdef POINTERS_EXTEND_UNSIGNED
12908 && (mode != Pmode || mem_mode == VOIDmode)
12909 #endif
12911 break;
12912 if (GET_CODE (rtl) == SYMBOL_REF
12913 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12915 dw_loc_descr_ref temp;
12917 /* If this is not defined, we have no way to emit the data. */
12918 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12919 break;
12921 temp = new_addr_loc_descr (rtl, dtprel_true);
12923 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12924 add_loc_descr (&mem_loc_result, temp);
12926 break;
12929 if (!const_ok_for_output (rtl))
12931 if (GET_CODE (rtl) == CONST)
12932 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12933 initialized);
12934 break;
12937 symref:
12938 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12939 vec_safe_push (used_rtx_array, rtl);
12940 break;
12942 case CONCAT:
12943 case CONCATN:
12944 case VAR_LOCATION:
12945 case DEBUG_IMPLICIT_PTR:
12946 expansion_failed (NULL_TREE, rtl,
12947 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12948 return 0;
12950 case ENTRY_VALUE:
12951 if (dwarf_strict)
12952 return NULL;
12953 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12955 if (GET_MODE_CLASS (mode) != MODE_INT
12956 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12957 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12958 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12959 else
12961 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12962 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12963 return NULL;
12964 op0 = one_reg_loc_descriptor (dbx_regnum,
12965 VAR_INIT_STATUS_INITIALIZED);
12968 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12969 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12971 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12972 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12973 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12974 return NULL;
12976 else
12977 gcc_unreachable ();
12978 if (op0 == NULL)
12979 return NULL;
12980 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12981 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12982 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12983 break;
12985 case DEBUG_PARAMETER_REF:
12986 mem_loc_result = parameter_ref_descriptor (rtl);
12987 break;
12989 case PRE_MODIFY:
12990 /* Extract the PLUS expression nested inside and fall into
12991 PLUS code below. */
12992 rtl = XEXP (rtl, 1);
12993 goto plus;
12995 case PRE_INC:
12996 case PRE_DEC:
12997 /* Turn these into a PLUS expression and fall into the PLUS code
12998 below. */
12999 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
13000 gen_int_mode (GET_CODE (rtl) == PRE_INC
13001 ? GET_MODE_UNIT_SIZE (mem_mode)
13002 : -GET_MODE_UNIT_SIZE (mem_mode),
13003 mode));
13005 /* ... fall through ... */
13007 case PLUS:
13008 plus:
13009 if (is_based_loc (rtl)
13010 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13011 || XEXP (rtl, 0) == arg_pointer_rtx
13012 || XEXP (rtl, 0) == frame_pointer_rtx)
13013 && GET_MODE_CLASS (mode) == MODE_INT)
13014 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13015 INTVAL (XEXP (rtl, 1)),
13016 VAR_INIT_STATUS_INITIALIZED);
13017 else
13019 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13020 VAR_INIT_STATUS_INITIALIZED);
13021 if (mem_loc_result == 0)
13022 break;
13024 if (CONST_INT_P (XEXP (rtl, 1))
13025 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13026 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13027 else
13029 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13030 VAR_INIT_STATUS_INITIALIZED);
13031 if (op1 == 0)
13032 return NULL;
13033 add_loc_descr (&mem_loc_result, op1);
13034 add_loc_descr (&mem_loc_result,
13035 new_loc_descr (DW_OP_plus, 0, 0));
13038 break;
13040 /* If a pseudo-reg is optimized away, it is possible for it to
13041 be replaced with a MEM containing a multiply or shift. */
13042 case MINUS:
13043 op = DW_OP_minus;
13044 goto do_binop;
13046 case MULT:
13047 op = DW_OP_mul;
13048 goto do_binop;
13050 case DIV:
13051 if (!dwarf_strict
13052 && GET_MODE_CLASS (mode) == MODE_INT
13053 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13055 mem_loc_result = typed_binop (DW_OP_div, rtl,
13056 base_type_for_mode (mode, 0),
13057 mode, mem_mode);
13058 break;
13060 op = DW_OP_div;
13061 goto do_binop;
13063 case UMOD:
13064 op = DW_OP_mod;
13065 goto do_binop;
13067 case ASHIFT:
13068 op = DW_OP_shl;
13069 goto do_shift;
13071 case ASHIFTRT:
13072 op = DW_OP_shra;
13073 goto do_shift;
13075 case LSHIFTRT:
13076 op = DW_OP_shr;
13077 goto do_shift;
13079 do_shift:
13080 if (GET_MODE_CLASS (mode) != MODE_INT)
13081 break;
13082 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13083 VAR_INIT_STATUS_INITIALIZED);
13085 rtx rtlop1 = XEXP (rtl, 1);
13086 if (GET_MODE (rtlop1) != VOIDmode
13087 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
13088 < GET_MODE_BITSIZE (mode))
13089 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13090 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13091 VAR_INIT_STATUS_INITIALIZED);
13094 if (op0 == 0 || op1 == 0)
13095 break;
13097 mem_loc_result = op0;
13098 add_loc_descr (&mem_loc_result, op1);
13099 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13100 break;
13102 case AND:
13103 op = DW_OP_and;
13104 goto do_binop;
13106 case IOR:
13107 op = DW_OP_or;
13108 goto do_binop;
13110 case XOR:
13111 op = DW_OP_xor;
13112 goto do_binop;
13114 do_binop:
13115 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13116 VAR_INIT_STATUS_INITIALIZED);
13117 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13118 VAR_INIT_STATUS_INITIALIZED);
13120 if (op0 == 0 || op1 == 0)
13121 break;
13123 mem_loc_result = op0;
13124 add_loc_descr (&mem_loc_result, op1);
13125 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13126 break;
13128 case MOD:
13129 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
13131 mem_loc_result = typed_binop (DW_OP_mod, rtl,
13132 base_type_for_mode (mode, 0),
13133 mode, mem_mode);
13134 break;
13137 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13138 VAR_INIT_STATUS_INITIALIZED);
13139 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13140 VAR_INIT_STATUS_INITIALIZED);
13142 if (op0 == 0 || op1 == 0)
13143 break;
13145 mem_loc_result = op0;
13146 add_loc_descr (&mem_loc_result, op1);
13147 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13148 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13149 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13150 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13151 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13152 break;
13154 case UDIV:
13155 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
13157 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13159 op = DW_OP_div;
13160 goto do_binop;
13162 mem_loc_result = typed_binop (DW_OP_div, rtl,
13163 base_type_for_mode (mode, 1),
13164 mode, mem_mode);
13166 break;
13168 case NOT:
13169 op = DW_OP_not;
13170 goto do_unop;
13172 case ABS:
13173 op = DW_OP_abs;
13174 goto do_unop;
13176 case NEG:
13177 op = DW_OP_neg;
13178 goto do_unop;
13180 do_unop:
13181 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13182 VAR_INIT_STATUS_INITIALIZED);
13184 if (op0 == 0)
13185 break;
13187 mem_loc_result = op0;
13188 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13189 break;
13191 case CONST_INT:
13192 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13193 #ifdef POINTERS_EXTEND_UNSIGNED
13194 || (mode == Pmode
13195 && mem_mode != VOIDmode
13196 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13197 #endif
13200 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13201 break;
13203 if (!dwarf_strict
13204 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13205 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13207 dw_die_ref type_die = base_type_for_mode (mode, 1);
13208 machine_mode amode;
13209 if (type_die == NULL)
13210 return NULL;
13211 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13212 MODE_INT, 0);
13213 if (INTVAL (rtl) >= 0
13214 && amode != BLKmode
13215 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13216 /* const DW_OP_GNU_convert <XXX> vs.
13217 DW_OP_GNU_const_type <XXX, 1, const>. */
13218 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13219 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13221 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13222 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13223 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13224 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13225 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13226 add_loc_descr (&mem_loc_result, op0);
13227 return mem_loc_result;
13229 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13230 INTVAL (rtl));
13231 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13232 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13233 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13234 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13235 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13236 else
13238 mem_loc_result->dw_loc_oprnd2.val_class
13239 = dw_val_class_const_double;
13240 mem_loc_result->dw_loc_oprnd2.v.val_double
13241 = double_int::from_shwi (INTVAL (rtl));
13244 break;
13246 case CONST_DOUBLE:
13247 if (!dwarf_strict)
13249 dw_die_ref type_die;
13251 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13252 CONST_DOUBLE rtx could represent either a large integer
13253 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
13254 the value is always a floating point constant.
13256 When it is an integer, a CONST_DOUBLE is used whenever
13257 the constant requires 2 HWIs to be adequately represented.
13258 We output CONST_DOUBLEs as blocks. */
13259 if (mode == VOIDmode
13260 || (GET_MODE (rtl) == VOIDmode
13261 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13262 break;
13263 type_die = base_type_for_mode (mode,
13264 GET_MODE_CLASS (mode) == MODE_INT);
13265 if (type_die == NULL)
13266 return NULL;
13267 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13268 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13269 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13270 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13271 #if TARGET_SUPPORTS_WIDE_INT == 0
13272 if (!SCALAR_FLOAT_MODE_P (mode))
13274 mem_loc_result->dw_loc_oprnd2.val_class
13275 = dw_val_class_const_double;
13276 mem_loc_result->dw_loc_oprnd2.v.val_double
13277 = rtx_to_double_int (rtl);
13279 else
13280 #endif
13282 unsigned int length = GET_MODE_SIZE (mode);
13283 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13285 insert_float (rtl, array);
13286 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13287 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13288 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13289 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13292 break;
13294 case CONST_WIDE_INT:
13295 if (!dwarf_strict)
13297 dw_die_ref type_die;
13299 type_die = base_type_for_mode (mode,
13300 GET_MODE_CLASS (mode) == MODE_INT);
13301 if (type_die == NULL)
13302 return NULL;
13303 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13304 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13305 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13306 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13307 mem_loc_result->dw_loc_oprnd2.val_class
13308 = dw_val_class_wide_int;
13309 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13310 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13312 break;
13314 case EQ:
13315 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13316 break;
13318 case GE:
13319 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13320 break;
13322 case GT:
13323 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13324 break;
13326 case LE:
13327 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13328 break;
13330 case LT:
13331 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13332 break;
13334 case NE:
13335 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13336 break;
13338 case GEU:
13339 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13340 break;
13342 case GTU:
13343 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13344 break;
13346 case LEU:
13347 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13348 break;
13350 case LTU:
13351 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13352 break;
13354 case UMIN:
13355 case UMAX:
13356 if (GET_MODE_CLASS (mode) != MODE_INT)
13357 break;
13358 /* FALLTHRU */
13359 case SMIN:
13360 case SMAX:
13361 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13362 break;
13364 case ZERO_EXTRACT:
13365 case SIGN_EXTRACT:
13366 if (CONST_INT_P (XEXP (rtl, 1))
13367 && CONST_INT_P (XEXP (rtl, 2))
13368 && ((unsigned) INTVAL (XEXP (rtl, 1))
13369 + (unsigned) INTVAL (XEXP (rtl, 2))
13370 <= GET_MODE_BITSIZE (mode))
13371 && GET_MODE_CLASS (mode) == MODE_INT
13372 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13373 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13375 int shift, size;
13376 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13377 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13378 if (op0 == 0)
13379 break;
13380 if (GET_CODE (rtl) == SIGN_EXTRACT)
13381 op = DW_OP_shra;
13382 else
13383 op = DW_OP_shr;
13384 mem_loc_result = op0;
13385 size = INTVAL (XEXP (rtl, 1));
13386 shift = INTVAL (XEXP (rtl, 2));
13387 if (BITS_BIG_ENDIAN)
13388 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13389 - shift - size;
13390 if (shift + size != (int) DWARF2_ADDR_SIZE)
13392 add_loc_descr (&mem_loc_result,
13393 int_loc_descriptor (DWARF2_ADDR_SIZE
13394 - shift - size));
13395 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13397 if (size != (int) DWARF2_ADDR_SIZE)
13399 add_loc_descr (&mem_loc_result,
13400 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13401 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13404 break;
13406 case IF_THEN_ELSE:
13408 dw_loc_descr_ref op2, bra_node, drop_node;
13409 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13410 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13411 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13412 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13413 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13414 VAR_INIT_STATUS_INITIALIZED);
13415 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13416 VAR_INIT_STATUS_INITIALIZED);
13417 if (op0 == NULL || op1 == NULL || op2 == NULL)
13418 break;
13420 mem_loc_result = op1;
13421 add_loc_descr (&mem_loc_result, op2);
13422 add_loc_descr (&mem_loc_result, op0);
13423 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13424 add_loc_descr (&mem_loc_result, bra_node);
13425 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13426 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13427 add_loc_descr (&mem_loc_result, drop_node);
13428 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13429 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13431 break;
13433 case FLOAT_EXTEND:
13434 case FLOAT_TRUNCATE:
13435 case FLOAT:
13436 case UNSIGNED_FLOAT:
13437 case FIX:
13438 case UNSIGNED_FIX:
13439 if (!dwarf_strict)
13441 dw_die_ref type_die;
13442 dw_loc_descr_ref cvt;
13444 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13445 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13446 if (op0 == NULL)
13447 break;
13448 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13449 && (GET_CODE (rtl) == FLOAT
13450 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13451 <= DWARF2_ADDR_SIZE))
13453 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13454 GET_CODE (rtl) == UNSIGNED_FLOAT);
13455 if (type_die == NULL)
13456 break;
13457 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13458 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13459 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13460 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13461 add_loc_descr (&op0, cvt);
13463 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13464 if (type_die == NULL)
13465 break;
13466 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13467 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13468 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13469 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13470 add_loc_descr (&op0, cvt);
13471 if (GET_MODE_CLASS (mode) == MODE_INT
13472 && (GET_CODE (rtl) == FIX
13473 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13475 op0 = convert_descriptor_to_mode (mode, op0);
13476 if (op0 == NULL)
13477 break;
13479 mem_loc_result = op0;
13481 break;
13483 case CLZ:
13484 case CTZ:
13485 case FFS:
13486 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13487 break;
13489 case POPCOUNT:
13490 case PARITY:
13491 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13492 break;
13494 case BSWAP:
13495 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13496 break;
13498 case ROTATE:
13499 case ROTATERT:
13500 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13501 break;
13503 case COMPARE:
13504 /* In theory, we could implement the above. */
13505 /* DWARF cannot represent the unsigned compare operations
13506 natively. */
13507 case SS_MULT:
13508 case US_MULT:
13509 case SS_DIV:
13510 case US_DIV:
13511 case SS_PLUS:
13512 case US_PLUS:
13513 case SS_MINUS:
13514 case US_MINUS:
13515 case SS_NEG:
13516 case US_NEG:
13517 case SS_ABS:
13518 case SS_ASHIFT:
13519 case US_ASHIFT:
13520 case SS_TRUNCATE:
13521 case US_TRUNCATE:
13522 case UNORDERED:
13523 case ORDERED:
13524 case UNEQ:
13525 case UNGE:
13526 case UNGT:
13527 case UNLE:
13528 case UNLT:
13529 case LTGT:
13530 case FRACT_CONVERT:
13531 case UNSIGNED_FRACT_CONVERT:
13532 case SAT_FRACT:
13533 case UNSIGNED_SAT_FRACT:
13534 case SQRT:
13535 case ASM_OPERANDS:
13536 case VEC_MERGE:
13537 case VEC_SELECT:
13538 case VEC_CONCAT:
13539 case VEC_DUPLICATE:
13540 case UNSPEC:
13541 case HIGH:
13542 case FMA:
13543 case STRICT_LOW_PART:
13544 case CONST_VECTOR:
13545 case CONST_FIXED:
13546 case CLRSB:
13547 case CLOBBER:
13548 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13549 can't express it in the debug info. This can happen e.g. with some
13550 TLS UNSPECs. */
13551 break;
13553 case CONST_STRING:
13554 resolve_one_addr (&rtl);
13555 goto symref;
13557 default:
13558 if (flag_checking)
13560 print_rtl (stderr, rtl);
13561 gcc_unreachable ();
13563 break;
13566 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13567 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13569 return mem_loc_result;
13572 /* Return a descriptor that describes the concatenation of two locations.
13573 This is typically a complex variable. */
13575 static dw_loc_descr_ref
13576 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13578 dw_loc_descr_ref cc_loc_result = NULL;
13579 dw_loc_descr_ref x0_ref
13580 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13581 dw_loc_descr_ref x1_ref
13582 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13584 if (x0_ref == 0 || x1_ref == 0)
13585 return 0;
13587 cc_loc_result = x0_ref;
13588 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13590 add_loc_descr (&cc_loc_result, x1_ref);
13591 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13593 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13594 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13596 return cc_loc_result;
13599 /* Return a descriptor that describes the concatenation of N
13600 locations. */
13602 static dw_loc_descr_ref
13603 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13605 unsigned int i;
13606 dw_loc_descr_ref cc_loc_result = NULL;
13607 unsigned int n = XVECLEN (concatn, 0);
13609 for (i = 0; i < n; ++i)
13611 dw_loc_descr_ref ref;
13612 rtx x = XVECEXP (concatn, 0, i);
13614 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13615 if (ref == NULL)
13616 return NULL;
13618 add_loc_descr (&cc_loc_result, ref);
13619 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13622 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13623 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13625 return cc_loc_result;
13628 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13629 for DEBUG_IMPLICIT_PTR RTL. */
13631 static dw_loc_descr_ref
13632 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13634 dw_loc_descr_ref ret;
13635 dw_die_ref ref;
13637 if (dwarf_strict)
13638 return NULL;
13639 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13640 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13641 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13642 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13643 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13644 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13645 if (ref)
13647 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13648 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13649 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13651 else
13653 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13654 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13656 return ret;
13659 /* Output a proper Dwarf location descriptor for a variable or parameter
13660 which is either allocated in a register or in a memory location. For a
13661 register, we just generate an OP_REG and the register number. For a
13662 memory location we provide a Dwarf postfix expression describing how to
13663 generate the (dynamic) address of the object onto the address stack.
13665 MODE is mode of the decl if this loc_descriptor is going to be used in
13666 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13667 allowed, VOIDmode otherwise.
13669 If we don't know how to describe it, return 0. */
13671 static dw_loc_descr_ref
13672 loc_descriptor (rtx rtl, machine_mode mode,
13673 enum var_init_status initialized)
13675 dw_loc_descr_ref loc_result = NULL;
13677 switch (GET_CODE (rtl))
13679 case SUBREG:
13680 /* The case of a subreg may arise when we have a local (register)
13681 variable or a formal (register) parameter which doesn't quite fill
13682 up an entire register. For now, just assume that it is
13683 legitimate to make the Dwarf info refer to the whole register which
13684 contains the given subreg. */
13685 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13686 loc_result = loc_descriptor (SUBREG_REG (rtl),
13687 GET_MODE (SUBREG_REG (rtl)), initialized);
13688 else
13689 goto do_default;
13690 break;
13692 case REG:
13693 loc_result = reg_loc_descriptor (rtl, initialized);
13694 break;
13696 case MEM:
13697 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13698 GET_MODE (rtl), initialized);
13699 if (loc_result == NULL)
13700 loc_result = tls_mem_loc_descriptor (rtl);
13701 if (loc_result == NULL)
13703 rtx new_rtl = avoid_constant_pool_reference (rtl);
13704 if (new_rtl != rtl)
13705 loc_result = loc_descriptor (new_rtl, mode, initialized);
13707 break;
13709 case CONCAT:
13710 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13711 initialized);
13712 break;
13714 case CONCATN:
13715 loc_result = concatn_loc_descriptor (rtl, initialized);
13716 break;
13718 case VAR_LOCATION:
13719 /* Single part. */
13720 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13722 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13723 if (GET_CODE (loc) == EXPR_LIST)
13724 loc = XEXP (loc, 0);
13725 loc_result = loc_descriptor (loc, mode, initialized);
13726 break;
13729 rtl = XEXP (rtl, 1);
13730 /* FALLTHRU */
13732 case PARALLEL:
13734 rtvec par_elems = XVEC (rtl, 0);
13735 int num_elem = GET_NUM_ELEM (par_elems);
13736 machine_mode mode;
13737 int i;
13739 /* Create the first one, so we have something to add to. */
13740 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13741 VOIDmode, initialized);
13742 if (loc_result == NULL)
13743 return NULL;
13744 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13745 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13746 for (i = 1; i < num_elem; i++)
13748 dw_loc_descr_ref temp;
13750 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13751 VOIDmode, initialized);
13752 if (temp == NULL)
13753 return NULL;
13754 add_loc_descr (&loc_result, temp);
13755 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13756 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13759 break;
13761 case CONST_INT:
13762 if (mode != VOIDmode && mode != BLKmode)
13763 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13764 INTVAL (rtl));
13765 break;
13767 case CONST_DOUBLE:
13768 if (mode == VOIDmode)
13769 mode = GET_MODE (rtl);
13771 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13773 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13775 /* Note that a CONST_DOUBLE rtx could represent either an integer
13776 or a floating-point constant. A CONST_DOUBLE is used whenever
13777 the constant requires more than one word in order to be
13778 adequately represented. We output CONST_DOUBLEs as blocks. */
13779 loc_result = new_loc_descr (DW_OP_implicit_value,
13780 GET_MODE_SIZE (mode), 0);
13781 #if TARGET_SUPPORTS_WIDE_INT == 0
13782 if (!SCALAR_FLOAT_MODE_P (mode))
13784 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13785 loc_result->dw_loc_oprnd2.v.val_double
13786 = rtx_to_double_int (rtl);
13788 else
13789 #endif
13791 unsigned int length = GET_MODE_SIZE (mode);
13792 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13794 insert_float (rtl, array);
13795 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13796 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13797 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13798 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13801 break;
13803 case CONST_WIDE_INT:
13804 if (mode == VOIDmode)
13805 mode = GET_MODE (rtl);
13807 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13809 loc_result = new_loc_descr (DW_OP_implicit_value,
13810 GET_MODE_SIZE (mode), 0);
13811 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13812 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13813 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13815 break;
13817 case CONST_VECTOR:
13818 if (mode == VOIDmode)
13819 mode = GET_MODE (rtl);
13821 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13823 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13824 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13825 unsigned char *array
13826 = ggc_vec_alloc<unsigned char> (length * elt_size);
13827 unsigned int i;
13828 unsigned char *p;
13829 machine_mode imode = GET_MODE_INNER (mode);
13831 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13832 switch (GET_MODE_CLASS (mode))
13834 case MODE_VECTOR_INT:
13835 for (i = 0, p = array; i < length; i++, p += elt_size)
13837 rtx elt = CONST_VECTOR_ELT (rtl, i);
13838 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13840 break;
13842 case MODE_VECTOR_FLOAT:
13843 for (i = 0, p = array; i < length; i++, p += elt_size)
13845 rtx elt = CONST_VECTOR_ELT (rtl, i);
13846 insert_float (elt, p);
13848 break;
13850 default:
13851 gcc_unreachable ();
13854 loc_result = new_loc_descr (DW_OP_implicit_value,
13855 length * elt_size, 0);
13856 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13857 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13858 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13859 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13861 break;
13863 case CONST:
13864 if (mode == VOIDmode
13865 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13866 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13867 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13869 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13870 break;
13872 /* FALLTHROUGH */
13873 case SYMBOL_REF:
13874 if (!const_ok_for_output (rtl))
13875 break;
13876 case LABEL_REF:
13877 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13878 && (dwarf_version >= 4 || !dwarf_strict))
13880 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13881 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13882 vec_safe_push (used_rtx_array, rtl);
13884 break;
13886 case DEBUG_IMPLICIT_PTR:
13887 loc_result = implicit_ptr_descriptor (rtl, 0);
13888 break;
13890 case PLUS:
13891 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13892 && CONST_INT_P (XEXP (rtl, 1)))
13894 loc_result
13895 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13896 break;
13898 /* FALLTHRU */
13899 do_default:
13900 default:
13901 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13902 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13903 && dwarf_version >= 4)
13904 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13906 /* Value expression. */
13907 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13908 if (loc_result)
13909 add_loc_descr (&loc_result,
13910 new_loc_descr (DW_OP_stack_value, 0, 0));
13912 break;
13915 return loc_result;
13918 /* We need to figure out what section we should use as the base for the
13919 address ranges where a given location is valid.
13920 1. If this particular DECL has a section associated with it, use that.
13921 2. If this function has a section associated with it, use that.
13922 3. Otherwise, use the text section.
13923 XXX: If you split a variable across multiple sections, we won't notice. */
13925 static const char *
13926 secname_for_decl (const_tree decl)
13928 const char *secname;
13930 if (VAR_OR_FUNCTION_DECL_P (decl)
13931 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13932 && DECL_SECTION_NAME (decl))
13933 secname = DECL_SECTION_NAME (decl);
13934 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13935 secname = DECL_SECTION_NAME (current_function_decl);
13936 else if (cfun && in_cold_section_p)
13937 secname = crtl->subsections.cold_section_label;
13938 else
13939 secname = text_section_label;
13941 return secname;
13944 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13946 static bool
13947 decl_by_reference_p (tree decl)
13949 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13950 || TREE_CODE (decl) == VAR_DECL)
13951 && DECL_BY_REFERENCE (decl));
13954 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13955 for VARLOC. */
13957 static dw_loc_descr_ref
13958 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13959 enum var_init_status initialized)
13961 int have_address = 0;
13962 dw_loc_descr_ref descr;
13963 machine_mode mode;
13965 if (want_address != 2)
13967 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13968 /* Single part. */
13969 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13971 varloc = PAT_VAR_LOCATION_LOC (varloc);
13972 if (GET_CODE (varloc) == EXPR_LIST)
13973 varloc = XEXP (varloc, 0);
13974 mode = GET_MODE (varloc);
13975 if (MEM_P (varloc))
13977 rtx addr = XEXP (varloc, 0);
13978 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13979 mode, initialized);
13980 if (descr)
13981 have_address = 1;
13982 else
13984 rtx x = avoid_constant_pool_reference (varloc);
13985 if (x != varloc)
13986 descr = mem_loc_descriptor (x, mode, VOIDmode,
13987 initialized);
13990 else
13991 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13993 else
13994 return 0;
13996 else
13998 if (GET_CODE (varloc) == VAR_LOCATION)
13999 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14000 else
14001 mode = DECL_MODE (loc);
14002 descr = loc_descriptor (varloc, mode, initialized);
14003 have_address = 1;
14006 if (!descr)
14007 return 0;
14009 if (want_address == 2 && !have_address
14010 && (dwarf_version >= 4 || !dwarf_strict))
14012 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14014 expansion_failed (loc, NULL_RTX,
14015 "DWARF address size mismatch");
14016 return 0;
14018 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14019 have_address = 1;
14021 /* Show if we can't fill the request for an address. */
14022 if (want_address && !have_address)
14024 expansion_failed (loc, NULL_RTX,
14025 "Want address and only have value");
14026 return 0;
14029 /* If we've got an address and don't want one, dereference. */
14030 if (!want_address && have_address)
14032 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14033 enum dwarf_location_atom op;
14035 if (size > DWARF2_ADDR_SIZE || size == -1)
14037 expansion_failed (loc, NULL_RTX,
14038 "DWARF address size mismatch");
14039 return 0;
14041 else if (size == DWARF2_ADDR_SIZE)
14042 op = DW_OP_deref;
14043 else
14044 op = DW_OP_deref_size;
14046 add_loc_descr (&descr, new_loc_descr (op, size, 0));
14049 return descr;
14052 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14053 if it is not possible. */
14055 static dw_loc_descr_ref
14056 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14058 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14059 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14060 else if (dwarf_version >= 3 || !dwarf_strict)
14061 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14062 else
14063 return NULL;
14066 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14067 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
14069 static dw_loc_descr_ref
14070 dw_sra_loc_expr (tree decl, rtx loc)
14072 rtx p;
14073 unsigned HOST_WIDE_INT padsize = 0;
14074 dw_loc_descr_ref descr, *descr_tail;
14075 unsigned HOST_WIDE_INT decl_size;
14076 rtx varloc;
14077 enum var_init_status initialized;
14079 if (DECL_SIZE (decl) == NULL
14080 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
14081 return NULL;
14083 decl_size = tree_to_uhwi (DECL_SIZE (decl));
14084 descr = NULL;
14085 descr_tail = &descr;
14087 for (p = loc; p; p = XEXP (p, 1))
14089 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
14090 rtx loc_note = *decl_piece_varloc_ptr (p);
14091 dw_loc_descr_ref cur_descr;
14092 dw_loc_descr_ref *tail, last = NULL;
14093 unsigned HOST_WIDE_INT opsize = 0;
14095 if (loc_note == NULL_RTX
14096 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14098 padsize += bitsize;
14099 continue;
14101 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14102 varloc = NOTE_VAR_LOCATION (loc_note);
14103 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14104 if (cur_descr == NULL)
14106 padsize += bitsize;
14107 continue;
14110 /* Check that cur_descr either doesn't use
14111 DW_OP_*piece operations, or their sum is equal
14112 to bitsize. Otherwise we can't embed it. */
14113 for (tail = &cur_descr; *tail != NULL;
14114 tail = &(*tail)->dw_loc_next)
14115 if ((*tail)->dw_loc_opc == DW_OP_piece)
14117 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14118 * BITS_PER_UNIT;
14119 last = *tail;
14121 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14123 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14124 last = *tail;
14127 if (last != NULL && opsize != bitsize)
14129 padsize += bitsize;
14130 /* Discard the current piece of the descriptor and release any
14131 addr_table entries it uses. */
14132 remove_loc_list_addr_table_entries (cur_descr);
14133 continue;
14136 /* If there is a hole, add DW_OP_*piece after empty DWARF
14137 expression, which means that those bits are optimized out. */
14138 if (padsize)
14140 if (padsize > decl_size)
14142 remove_loc_list_addr_table_entries (cur_descr);
14143 goto discard_descr;
14145 decl_size -= padsize;
14146 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14147 if (*descr_tail == NULL)
14149 remove_loc_list_addr_table_entries (cur_descr);
14150 goto discard_descr;
14152 descr_tail = &(*descr_tail)->dw_loc_next;
14153 padsize = 0;
14155 *descr_tail = cur_descr;
14156 descr_tail = tail;
14157 if (bitsize > decl_size)
14158 goto discard_descr;
14159 decl_size -= bitsize;
14160 if (last == NULL)
14162 HOST_WIDE_INT offset = 0;
14163 if (GET_CODE (varloc) == VAR_LOCATION
14164 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14166 varloc = PAT_VAR_LOCATION_LOC (varloc);
14167 if (GET_CODE (varloc) == EXPR_LIST)
14168 varloc = XEXP (varloc, 0);
14172 if (GET_CODE (varloc) == CONST
14173 || GET_CODE (varloc) == SIGN_EXTEND
14174 || GET_CODE (varloc) == ZERO_EXTEND)
14175 varloc = XEXP (varloc, 0);
14176 else if (GET_CODE (varloc) == SUBREG)
14177 varloc = SUBREG_REG (varloc);
14178 else
14179 break;
14181 while (1);
14182 /* DW_OP_bit_size offset should be zero for register
14183 or implicit location descriptions and empty location
14184 descriptions, but for memory addresses needs big endian
14185 adjustment. */
14186 if (MEM_P (varloc))
14188 unsigned HOST_WIDE_INT memsize
14189 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14190 if (memsize != bitsize)
14192 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14193 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14194 goto discard_descr;
14195 if (memsize < bitsize)
14196 goto discard_descr;
14197 if (BITS_BIG_ENDIAN)
14198 offset = memsize - bitsize;
14202 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14203 if (*descr_tail == NULL)
14204 goto discard_descr;
14205 descr_tail = &(*descr_tail)->dw_loc_next;
14209 /* If there were any non-empty expressions, add padding till the end of
14210 the decl. */
14211 if (descr != NULL && decl_size != 0)
14213 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14214 if (*descr_tail == NULL)
14215 goto discard_descr;
14217 return descr;
14219 discard_descr:
14220 /* Discard the descriptor and release any addr_table entries it uses. */
14221 remove_loc_list_addr_table_entries (descr);
14222 return NULL;
14225 /* Return the dwarf representation of the location list LOC_LIST of
14226 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
14227 function. */
14229 static dw_loc_list_ref
14230 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14232 const char *endname, *secname;
14233 rtx varloc;
14234 enum var_init_status initialized;
14235 struct var_loc_node *node;
14236 dw_loc_descr_ref descr;
14237 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14238 dw_loc_list_ref list = NULL;
14239 dw_loc_list_ref *listp = &list;
14241 /* Now that we know what section we are using for a base,
14242 actually construct the list of locations.
14243 The first location information is what is passed to the
14244 function that creates the location list, and the remaining
14245 locations just get added on to that list.
14246 Note that we only know the start address for a location
14247 (IE location changes), so to build the range, we use
14248 the range [current location start, next location start].
14249 This means we have to special case the last node, and generate
14250 a range of [last location start, end of function label]. */
14252 secname = secname_for_decl (decl);
14254 for (node = loc_list->first; node; node = node->next)
14255 if (GET_CODE (node->loc) == EXPR_LIST
14256 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14258 if (GET_CODE (node->loc) == EXPR_LIST)
14260 /* This requires DW_OP_{,bit_}piece, which is not usable
14261 inside DWARF expressions. */
14262 if (want_address != 2)
14263 continue;
14264 descr = dw_sra_loc_expr (decl, node->loc);
14265 if (descr == NULL)
14266 continue;
14268 else
14270 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14271 varloc = NOTE_VAR_LOCATION (node->loc);
14272 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14274 if (descr)
14276 bool range_across_switch = false;
14277 /* If section switch happens in between node->label
14278 and node->next->label (or end of function) and
14279 we can't emit it as a single entry list,
14280 emit two ranges, first one ending at the end
14281 of first partition and second one starting at the
14282 beginning of second partition. */
14283 if (node == loc_list->last_before_switch
14284 && (node != loc_list->first || loc_list->first->next)
14285 && current_function_decl)
14287 endname = cfun->fde->dw_fde_end;
14288 range_across_switch = true;
14290 /* The variable has a location between NODE->LABEL and
14291 NODE->NEXT->LABEL. */
14292 else if (node->next)
14293 endname = node->next->label;
14294 /* If the variable has a location at the last label
14295 it keeps its location until the end of function. */
14296 else if (!current_function_decl)
14297 endname = text_end_label;
14298 else
14300 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14301 current_function_funcdef_no);
14302 endname = ggc_strdup (label_id);
14305 *listp = new_loc_list (descr, node->label, endname, secname);
14306 if (TREE_CODE (decl) == PARM_DECL
14307 && node == loc_list->first
14308 && NOTE_P (node->loc)
14309 && strcmp (node->label, endname) == 0)
14310 (*listp)->force = true;
14311 listp = &(*listp)->dw_loc_next;
14313 if (range_across_switch)
14315 if (GET_CODE (node->loc) == EXPR_LIST)
14316 descr = dw_sra_loc_expr (decl, node->loc);
14317 else
14319 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14320 varloc = NOTE_VAR_LOCATION (node->loc);
14321 descr = dw_loc_list_1 (decl, varloc, want_address,
14322 initialized);
14324 gcc_assert (descr);
14325 /* The variable has a location between NODE->LABEL and
14326 NODE->NEXT->LABEL. */
14327 if (node->next)
14328 endname = node->next->label;
14329 else
14330 endname = cfun->fde->dw_fde_second_end;
14331 *listp = new_loc_list (descr,
14332 cfun->fde->dw_fde_second_begin,
14333 endname, secname);
14334 listp = &(*listp)->dw_loc_next;
14339 /* Try to avoid the overhead of a location list emitting a location
14340 expression instead, but only if we didn't have more than one
14341 location entry in the first place. If some entries were not
14342 representable, we don't want to pretend a single entry that was
14343 applies to the entire scope in which the variable is
14344 available. */
14345 if (list && loc_list->first->next)
14346 gen_llsym (list);
14348 return list;
14351 /* Return if the loc_list has only single element and thus can be represented
14352 as location description. */
14354 static bool
14355 single_element_loc_list_p (dw_loc_list_ref list)
14357 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14358 return !list->ll_symbol;
14361 /* To each location in list LIST add loc descr REF. */
14363 static void
14364 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14366 dw_loc_descr_ref copy;
14367 add_loc_descr (&list->expr, ref);
14368 list = list->dw_loc_next;
14369 while (list)
14371 copy = ggc_alloc<dw_loc_descr_node> ();
14372 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14373 add_loc_descr (&list->expr, copy);
14374 while (copy->dw_loc_next)
14376 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14377 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14378 copy->dw_loc_next = new_copy;
14379 copy = new_copy;
14381 list = list->dw_loc_next;
14385 /* Given two lists RET and LIST
14386 produce location list that is result of adding expression in LIST
14387 to expression in RET on each position in program.
14388 Might be destructive on both RET and LIST.
14390 TODO: We handle only simple cases of RET or LIST having at most one
14391 element. General case would inolve sorting the lists in program order
14392 and merging them that will need some additional work.
14393 Adding that will improve quality of debug info especially for SRA-ed
14394 structures. */
14396 static void
14397 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14399 if (!list)
14400 return;
14401 if (!*ret)
14403 *ret = list;
14404 return;
14406 if (!list->dw_loc_next)
14408 add_loc_descr_to_each (*ret, list->expr);
14409 return;
14411 if (!(*ret)->dw_loc_next)
14413 add_loc_descr_to_each (list, (*ret)->expr);
14414 *ret = list;
14415 return;
14417 expansion_failed (NULL_TREE, NULL_RTX,
14418 "Don't know how to merge two non-trivial"
14419 " location lists.\n");
14420 *ret = NULL;
14421 return;
14424 /* LOC is constant expression. Try a luck, look it up in constant
14425 pool and return its loc_descr of its address. */
14427 static dw_loc_descr_ref
14428 cst_pool_loc_descr (tree loc)
14430 /* Get an RTL for this, if something has been emitted. */
14431 rtx rtl = lookup_constant_def (loc);
14433 if (!rtl || !MEM_P (rtl))
14435 gcc_assert (!rtl);
14436 return 0;
14438 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14440 /* TODO: We might get more coverage if we was actually delaying expansion
14441 of all expressions till end of compilation when constant pools are fully
14442 populated. */
14443 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14445 expansion_failed (loc, NULL_RTX,
14446 "CST value in contant pool but not marked.");
14447 return 0;
14449 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14450 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14453 /* Return dw_loc_list representing address of addr_expr LOC
14454 by looking for inner INDIRECT_REF expression and turning
14455 it into simple arithmetics.
14457 See loc_list_from_tree for the meaning of CONTEXT. */
14459 static dw_loc_list_ref
14460 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
14461 const loc_descr_context *context)
14463 tree obj, offset;
14464 HOST_WIDE_INT bitsize, bitpos, bytepos;
14465 machine_mode mode;
14466 int unsignedp, volatilep = 0;
14467 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14469 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14470 &bitsize, &bitpos, &offset, &mode,
14471 &unsignedp, &volatilep, false);
14472 STRIP_NOPS (obj);
14473 if (bitpos % BITS_PER_UNIT)
14475 expansion_failed (loc, NULL_RTX, "bitfield access");
14476 return 0;
14478 if (!INDIRECT_REF_P (obj))
14480 expansion_failed (obj,
14481 NULL_RTX, "no indirect ref in inner refrence");
14482 return 0;
14484 if (!offset && !bitpos)
14485 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
14486 context);
14487 else if (toplev
14488 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14489 && (dwarf_version >= 4 || !dwarf_strict))
14491 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
14492 if (!list_ret)
14493 return 0;
14494 if (offset)
14496 /* Variable offset. */
14497 list_ret1 = loc_list_from_tree (offset, 0, context);
14498 if (list_ret1 == 0)
14499 return 0;
14500 add_loc_list (&list_ret, list_ret1);
14501 if (!list_ret)
14502 return 0;
14503 add_loc_descr_to_each (list_ret,
14504 new_loc_descr (DW_OP_plus, 0, 0));
14506 bytepos = bitpos / BITS_PER_UNIT;
14507 if (bytepos > 0)
14508 add_loc_descr_to_each (list_ret,
14509 new_loc_descr (DW_OP_plus_uconst,
14510 bytepos, 0));
14511 else if (bytepos < 0)
14512 loc_list_plus_const (list_ret, bytepos);
14513 add_loc_descr_to_each (list_ret,
14514 new_loc_descr (DW_OP_stack_value, 0, 0));
14516 return list_ret;
14520 /* Helper structure for location descriptions generation. */
14521 struct loc_descr_context
14523 /* The type that is implicitly referenced by DW_OP_push_object_address, or
14524 NULL_TREE if DW_OP_push_object_address in invalid for this location
14525 description. This is used when processing PLACEHOLDER_EXPR nodes. */
14526 tree context_type;
14527 /* The ..._DECL node that should be translated as a
14528 DW_OP_push_object_address operation. */
14529 tree base_decl;
14532 /* Generate Dwarf location list representing LOC.
14533 If WANT_ADDRESS is false, expression computing LOC will be computed
14534 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14535 if WANT_ADDRESS is 2, expression computing address useable in location
14536 will be returned (i.e. DW_OP_reg can be used
14537 to refer to register values).
14539 CONTEXT provides information to customize the location descriptions
14540 generation. Its context_type field specifies what type is implicitly
14541 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
14542 will not be generated.
14544 If CONTEXT is NULL, the behavior is the same as if both context_type and
14545 base_decl fields were NULL_TREE. */
14547 static dw_loc_list_ref
14548 loc_list_from_tree (tree loc, int want_address,
14549 const struct loc_descr_context *context)
14551 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14552 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14553 int have_address = 0;
14554 enum dwarf_location_atom op;
14556 /* ??? Most of the time we do not take proper care for sign/zero
14557 extending the values properly. Hopefully this won't be a real
14558 problem... */
14560 if (context != NULL
14561 && context->base_decl == loc
14562 && want_address == 0)
14564 if (dwarf_version >= 3 || !dwarf_strict)
14565 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
14566 NULL, NULL, NULL);
14567 else
14568 return NULL;
14571 switch (TREE_CODE (loc))
14573 case ERROR_MARK:
14574 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14575 return 0;
14577 case PLACEHOLDER_EXPR:
14578 /* This case involves extracting fields from an object to determine the
14579 position of other fields. It is supposed to appear only as the first
14580 operand of COMPONENT_REF nodes and to reference precisely the type
14581 that the context allows. */
14582 if (context != NULL
14583 && TREE_TYPE (loc) == context->context_type
14584 && want_address >= 1)
14586 if (dwarf_version >= 3 || !dwarf_strict)
14588 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
14589 have_address = 1;
14590 break;
14592 else
14593 return NULL;
14595 else
14596 expansion_failed (loc, NULL_RTX,
14597 "PLACEHOLDER_EXPR for an unexpected type");
14598 break;
14600 case CALL_EXPR:
14601 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14602 /* There are no opcodes for these operations. */
14603 return 0;
14605 case PREINCREMENT_EXPR:
14606 case PREDECREMENT_EXPR:
14607 case POSTINCREMENT_EXPR:
14608 case POSTDECREMENT_EXPR:
14609 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14610 /* There are no opcodes for these operations. */
14611 return 0;
14613 case ADDR_EXPR:
14614 /* If we already want an address, see if there is INDIRECT_REF inside
14615 e.g. for &this->field. */
14616 if (want_address)
14618 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14619 (loc, want_address == 2, context);
14620 if (list_ret)
14621 have_address = 1;
14622 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14623 && (ret = cst_pool_loc_descr (loc)))
14624 have_address = 1;
14626 /* Otherwise, process the argument and look for the address. */
14627 if (!list_ret && !ret)
14628 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1, context);
14629 else
14631 if (want_address)
14632 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14633 return NULL;
14635 break;
14637 case VAR_DECL:
14638 if (DECL_THREAD_LOCAL_P (loc))
14640 rtx rtl;
14641 enum dwarf_location_atom tls_op;
14642 enum dtprel_bool dtprel = dtprel_false;
14644 if (targetm.have_tls)
14646 /* If this is not defined, we have no way to emit the
14647 data. */
14648 if (!targetm.asm_out.output_dwarf_dtprel)
14649 return 0;
14651 /* The way DW_OP_GNU_push_tls_address is specified, we
14652 can only look up addresses of objects in the current
14653 module. We used DW_OP_addr as first op, but that's
14654 wrong, because DW_OP_addr is relocated by the debug
14655 info consumer, while DW_OP_GNU_push_tls_address
14656 operand shouldn't be. */
14657 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14658 return 0;
14659 dtprel = dtprel_true;
14660 tls_op = DW_OP_GNU_push_tls_address;
14662 else
14664 if (!targetm.emutls.debug_form_tls_address
14665 || !(dwarf_version >= 3 || !dwarf_strict))
14666 return 0;
14667 /* We stuffed the control variable into the DECL_VALUE_EXPR
14668 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14669 no longer appear in gimple code. We used the control
14670 variable in specific so that we could pick it up here. */
14671 loc = DECL_VALUE_EXPR (loc);
14672 tls_op = DW_OP_form_tls_address;
14675 rtl = rtl_for_decl_location (loc);
14676 if (rtl == NULL_RTX)
14677 return 0;
14679 if (!MEM_P (rtl))
14680 return 0;
14681 rtl = XEXP (rtl, 0);
14682 if (! CONSTANT_P (rtl))
14683 return 0;
14685 ret = new_addr_loc_descr (rtl, dtprel);
14686 ret1 = new_loc_descr (tls_op, 0, 0);
14687 add_loc_descr (&ret, ret1);
14689 have_address = 1;
14690 break;
14692 /* FALLTHRU */
14694 case PARM_DECL:
14695 case RESULT_DECL:
14696 if (DECL_HAS_VALUE_EXPR_P (loc))
14697 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14698 want_address, context);
14699 /* FALLTHRU */
14701 case FUNCTION_DECL:
14703 rtx rtl;
14704 var_loc_list *loc_list = lookup_decl_loc (loc);
14706 if (loc_list && loc_list->first)
14708 list_ret = dw_loc_list (loc_list, loc, want_address);
14709 have_address = want_address != 0;
14710 break;
14712 rtl = rtl_for_decl_location (loc);
14713 if (rtl == NULL_RTX)
14715 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14716 return 0;
14718 else if (CONST_INT_P (rtl))
14720 HOST_WIDE_INT val = INTVAL (rtl);
14721 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14722 val &= GET_MODE_MASK (DECL_MODE (loc));
14723 ret = int_loc_descriptor (val);
14725 else if (GET_CODE (rtl) == CONST_STRING)
14727 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14728 return 0;
14730 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14731 ret = new_addr_loc_descr (rtl, dtprel_false);
14732 else
14734 machine_mode mode, mem_mode;
14736 /* Certain constructs can only be represented at top-level. */
14737 if (want_address == 2)
14739 ret = loc_descriptor (rtl, VOIDmode,
14740 VAR_INIT_STATUS_INITIALIZED);
14741 have_address = 1;
14743 else
14745 mode = GET_MODE (rtl);
14746 mem_mode = VOIDmode;
14747 if (MEM_P (rtl))
14749 mem_mode = mode;
14750 mode = get_address_mode (rtl);
14751 rtl = XEXP (rtl, 0);
14752 have_address = 1;
14754 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14755 VAR_INIT_STATUS_INITIALIZED);
14757 if (!ret)
14758 expansion_failed (loc, rtl,
14759 "failed to produce loc descriptor for rtl");
14762 break;
14764 case MEM_REF:
14765 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14767 have_address = 1;
14768 goto do_plus;
14770 /* Fallthru. */
14771 case INDIRECT_REF:
14772 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14773 have_address = 1;
14774 break;
14776 case TARGET_MEM_REF:
14777 case SSA_NAME:
14778 case DEBUG_EXPR_DECL:
14779 return NULL;
14781 case COMPOUND_EXPR:
14782 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address, context);
14784 CASE_CONVERT:
14785 case VIEW_CONVERT_EXPR:
14786 case SAVE_EXPR:
14787 case MODIFY_EXPR:
14788 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address, context);
14790 case COMPONENT_REF:
14791 case BIT_FIELD_REF:
14792 case ARRAY_REF:
14793 case ARRAY_RANGE_REF:
14794 case REALPART_EXPR:
14795 case IMAGPART_EXPR:
14797 tree obj, offset;
14798 HOST_WIDE_INT bitsize, bitpos, bytepos;
14799 machine_mode mode;
14800 int unsignedp, volatilep = 0;
14802 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14803 &unsignedp, &volatilep, false);
14805 gcc_assert (obj != loc);
14807 list_ret = loc_list_from_tree (obj,
14808 want_address == 2
14809 && !bitpos && !offset ? 2 : 1,
14810 context);
14811 /* TODO: We can extract value of the small expression via shifting even
14812 for nonzero bitpos. */
14813 if (list_ret == 0)
14814 return 0;
14815 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14817 expansion_failed (loc, NULL_RTX,
14818 "bitfield access");
14819 return 0;
14822 if (offset != NULL_TREE)
14824 /* Variable offset. */
14825 list_ret1 = loc_list_from_tree (offset, 0, context);
14826 if (list_ret1 == 0)
14827 return 0;
14828 add_loc_list (&list_ret, list_ret1);
14829 if (!list_ret)
14830 return 0;
14831 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14834 bytepos = bitpos / BITS_PER_UNIT;
14835 if (bytepos > 0)
14836 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14837 else if (bytepos < 0)
14838 loc_list_plus_const (list_ret, bytepos);
14840 have_address = 1;
14841 break;
14844 case INTEGER_CST:
14845 if ((want_address || !tree_fits_shwi_p (loc))
14846 && (ret = cst_pool_loc_descr (loc)))
14847 have_address = 1;
14848 else if (want_address == 2
14849 && tree_fits_shwi_p (loc)
14850 && (ret = address_of_int_loc_descriptor
14851 (int_size_in_bytes (TREE_TYPE (loc)),
14852 tree_to_shwi (loc))))
14853 have_address = 1;
14854 else if (tree_fits_shwi_p (loc))
14855 ret = int_loc_descriptor (tree_to_shwi (loc));
14856 else
14858 expansion_failed (loc, NULL_RTX,
14859 "Integer operand is not host integer");
14860 return 0;
14862 break;
14864 case CONSTRUCTOR:
14865 case REAL_CST:
14866 case STRING_CST:
14867 case COMPLEX_CST:
14868 if ((ret = cst_pool_loc_descr (loc)))
14869 have_address = 1;
14870 else
14871 /* We can construct small constants here using int_loc_descriptor. */
14872 expansion_failed (loc, NULL_RTX,
14873 "constructor or constant not in constant pool");
14874 break;
14876 case TRUTH_AND_EXPR:
14877 case TRUTH_ANDIF_EXPR:
14878 case BIT_AND_EXPR:
14879 op = DW_OP_and;
14880 goto do_binop;
14882 case TRUTH_XOR_EXPR:
14883 case BIT_XOR_EXPR:
14884 op = DW_OP_xor;
14885 goto do_binop;
14887 case TRUTH_OR_EXPR:
14888 case TRUTH_ORIF_EXPR:
14889 case BIT_IOR_EXPR:
14890 op = DW_OP_or;
14891 goto do_binop;
14893 case FLOOR_DIV_EXPR:
14894 case CEIL_DIV_EXPR:
14895 case ROUND_DIV_EXPR:
14896 case TRUNC_DIV_EXPR:
14897 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14898 return 0;
14899 op = DW_OP_div;
14900 goto do_binop;
14902 case MINUS_EXPR:
14903 op = DW_OP_minus;
14904 goto do_binop;
14906 case FLOOR_MOD_EXPR:
14907 case CEIL_MOD_EXPR:
14908 case ROUND_MOD_EXPR:
14909 case TRUNC_MOD_EXPR:
14910 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14912 op = DW_OP_mod;
14913 goto do_binop;
14915 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14916 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14917 if (list_ret == 0 || list_ret1 == 0)
14918 return 0;
14920 add_loc_list (&list_ret, list_ret1);
14921 if (list_ret == 0)
14922 return 0;
14923 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14924 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14925 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14926 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14927 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14928 break;
14930 case MULT_EXPR:
14931 op = DW_OP_mul;
14932 goto do_binop;
14934 case LSHIFT_EXPR:
14935 op = DW_OP_shl;
14936 goto do_binop;
14938 case RSHIFT_EXPR:
14939 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14940 goto do_binop;
14942 case POINTER_PLUS_EXPR:
14943 case PLUS_EXPR:
14944 do_plus:
14945 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14947 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14948 if (list_ret == 0)
14949 return 0;
14951 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14952 break;
14955 op = DW_OP_plus;
14956 goto do_binop;
14958 case LE_EXPR:
14959 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14960 return 0;
14962 op = DW_OP_le;
14963 goto do_binop;
14965 case GE_EXPR:
14966 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14967 return 0;
14969 op = DW_OP_ge;
14970 goto do_binop;
14972 case LT_EXPR:
14973 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14974 return 0;
14976 op = DW_OP_lt;
14977 goto do_binop;
14979 case GT_EXPR:
14980 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14981 return 0;
14983 op = DW_OP_gt;
14984 goto do_binop;
14986 case EQ_EXPR:
14987 op = DW_OP_eq;
14988 goto do_binop;
14990 case NE_EXPR:
14991 op = DW_OP_ne;
14992 goto do_binop;
14994 do_binop:
14995 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14996 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14997 if (list_ret == 0 || list_ret1 == 0)
14998 return 0;
15000 add_loc_list (&list_ret, list_ret1);
15001 if (list_ret == 0)
15002 return 0;
15003 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15004 break;
15006 case TRUTH_NOT_EXPR:
15007 case BIT_NOT_EXPR:
15008 op = DW_OP_not;
15009 goto do_unop;
15011 case ABS_EXPR:
15012 op = DW_OP_abs;
15013 goto do_unop;
15015 case NEGATE_EXPR:
15016 op = DW_OP_neg;
15017 goto do_unop;
15019 do_unop:
15020 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
15021 if (list_ret == 0)
15022 return 0;
15024 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15025 break;
15027 case MIN_EXPR:
15028 case MAX_EXPR:
15030 const enum tree_code code =
15031 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15033 loc = build3 (COND_EXPR, TREE_TYPE (loc),
15034 build2 (code, integer_type_node,
15035 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15036 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15039 /* ... fall through ... */
15041 case COND_EXPR:
15043 dw_loc_descr_ref lhs
15044 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
15045 dw_loc_list_ref rhs
15046 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0, context);
15047 dw_loc_descr_ref bra_node, jump_node, tmp;
15049 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
15050 if (list_ret == 0 || lhs == 0 || rhs == 0)
15051 return 0;
15053 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15054 add_loc_descr_to_each (list_ret, bra_node);
15056 add_loc_list (&list_ret, rhs);
15057 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15058 add_loc_descr_to_each (list_ret, jump_node);
15060 add_loc_descr_to_each (list_ret, lhs);
15061 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15062 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15064 /* ??? Need a node to point the skip at. Use a nop. */
15065 tmp = new_loc_descr (DW_OP_nop, 0, 0);
15066 add_loc_descr_to_each (list_ret, tmp);
15067 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15068 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15070 break;
15072 case FIX_TRUNC_EXPR:
15073 return 0;
15075 default:
15076 /* Leave front-end specific codes as simply unknown. This comes
15077 up, for instance, with the C STMT_EXPR. */
15078 if ((unsigned int) TREE_CODE (loc)
15079 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15081 expansion_failed (loc, NULL_RTX,
15082 "language specific tree node");
15083 return 0;
15086 /* Otherwise this is a generic code; we should just lists all of
15087 these explicitly. We forgot one. */
15088 if (flag_checking)
15089 gcc_unreachable ();
15091 /* In a release build, we want to degrade gracefully: better to
15092 generate incomplete debugging information than to crash. */
15093 return NULL;
15096 if (!ret && !list_ret)
15097 return 0;
15099 if (want_address == 2 && !have_address
15100 && (dwarf_version >= 4 || !dwarf_strict))
15102 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15104 expansion_failed (loc, NULL_RTX,
15105 "DWARF address size mismatch");
15106 return 0;
15108 if (ret)
15109 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15110 else
15111 add_loc_descr_to_each (list_ret,
15112 new_loc_descr (DW_OP_stack_value, 0, 0));
15113 have_address = 1;
15115 /* Show if we can't fill the request for an address. */
15116 if (want_address && !have_address)
15118 expansion_failed (loc, NULL_RTX,
15119 "Want address and only have value");
15120 return 0;
15123 gcc_assert (!ret || !list_ret);
15125 /* If we've got an address and don't want one, dereference. */
15126 if (!want_address && have_address)
15128 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15130 if (size > DWARF2_ADDR_SIZE || size == -1)
15132 expansion_failed (loc, NULL_RTX,
15133 "DWARF address size mismatch");
15134 return 0;
15136 else if (size == DWARF2_ADDR_SIZE)
15137 op = DW_OP_deref;
15138 else
15139 op = DW_OP_deref_size;
15141 if (ret)
15142 add_loc_descr (&ret, new_loc_descr (op, size, 0));
15143 else
15144 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15146 if (ret)
15147 list_ret = new_loc_list (ret, NULL, NULL, NULL);
15149 return list_ret;
15152 /* Same as above but return only single location expression. */
15153 static dw_loc_descr_ref
15154 loc_descriptor_from_tree (tree loc, int want_address,
15155 const struct loc_descr_context *context)
15157 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
15158 if (!ret)
15159 return NULL;
15160 if (ret->dw_loc_next)
15162 expansion_failed (loc, NULL_RTX,
15163 "Location list where only loc descriptor needed");
15164 return NULL;
15166 return ret->expr;
15169 /* Given a value, round it up to the lowest multiple of `boundary'
15170 which is not less than the value itself. */
15172 static inline HOST_WIDE_INT
15173 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15175 return (((value + boundary - 1) / boundary) * boundary);
15178 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15179 pointer to the declared type for the relevant field variable, or return
15180 `integer_type_node' if the given node turns out to be an
15181 ERROR_MARK node. */
15183 static inline tree
15184 field_type (const_tree decl)
15186 tree type;
15188 if (TREE_CODE (decl) == ERROR_MARK)
15189 return integer_type_node;
15191 type = DECL_BIT_FIELD_TYPE (decl);
15192 if (type == NULL_TREE)
15193 type = TREE_TYPE (decl);
15195 return type;
15198 /* Given a pointer to a tree node, return the alignment in bits for
15199 it, or else return BITS_PER_WORD if the node actually turns out to
15200 be an ERROR_MARK node. */
15202 static inline unsigned
15203 simple_type_align_in_bits (const_tree type)
15205 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15208 static inline unsigned
15209 simple_decl_align_in_bits (const_tree decl)
15211 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15214 /* Return the result of rounding T up to ALIGN. */
15216 static inline offset_int
15217 round_up_to_align (const offset_int &t, unsigned int align)
15219 return wi::udiv_trunc (t + align - 1, align) * align;
15222 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15223 lowest addressed byte of the "containing object" for the given FIELD_DECL,
15224 or return 0 if we are unable to determine what that offset is, either
15225 because the argument turns out to be a pointer to an ERROR_MARK node, or
15226 because the offset is actually variable. (We can't handle the latter case
15227 just yet). */
15229 static HOST_WIDE_INT
15230 field_byte_offset (const_tree decl)
15232 offset_int object_offset_in_bits;
15233 offset_int object_offset_in_bytes;
15234 offset_int bitpos_int;
15236 if (TREE_CODE (decl) == ERROR_MARK)
15237 return 0;
15239 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15241 /* We cannot yet cope with fields whose positions are variable, so
15242 for now, when we see such things, we simply return 0. Someday, we may
15243 be able to handle such cases, but it will be damn difficult. */
15244 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15245 return 0;
15247 bitpos_int = wi::to_offset (bit_position (decl));
15249 if (PCC_BITFIELD_TYPE_MATTERS)
15251 tree type;
15252 tree field_size_tree;
15253 offset_int deepest_bitpos;
15254 offset_int field_size_in_bits;
15255 unsigned int type_align_in_bits;
15256 unsigned int decl_align_in_bits;
15257 offset_int type_size_in_bits;
15259 type = field_type (decl);
15260 type_size_in_bits = offset_int_type_size_in_bits (type);
15261 type_align_in_bits = simple_type_align_in_bits (type);
15263 field_size_tree = DECL_SIZE (decl);
15265 /* The size could be unspecified if there was an error, or for
15266 a flexible array member. */
15267 if (!field_size_tree)
15268 field_size_tree = bitsize_zero_node;
15270 /* If the size of the field is not constant, use the type size. */
15271 if (TREE_CODE (field_size_tree) == INTEGER_CST)
15272 field_size_in_bits = wi::to_offset (field_size_tree);
15273 else
15274 field_size_in_bits = type_size_in_bits;
15276 decl_align_in_bits = simple_decl_align_in_bits (decl);
15278 /* The GCC front-end doesn't make any attempt to keep track of the
15279 starting bit offset (relative to the start of the containing
15280 structure type) of the hypothetical "containing object" for a
15281 bit-field. Thus, when computing the byte offset value for the
15282 start of the "containing object" of a bit-field, we must deduce
15283 this information on our own. This can be rather tricky to do in
15284 some cases. For example, handling the following structure type
15285 definition when compiling for an i386/i486 target (which only
15286 aligns long long's to 32-bit boundaries) can be very tricky:
15288 struct S { int field1; long long field2:31; };
15290 Fortunately, there is a simple rule-of-thumb which can be used
15291 in such cases. When compiling for an i386/i486, GCC will
15292 allocate 8 bytes for the structure shown above. It decides to
15293 do this based upon one simple rule for bit-field allocation.
15294 GCC allocates each "containing object" for each bit-field at
15295 the first (i.e. lowest addressed) legitimate alignment boundary
15296 (based upon the required minimum alignment for the declared
15297 type of the field) which it can possibly use, subject to the
15298 condition that there is still enough available space remaining
15299 in the containing object (when allocated at the selected point)
15300 to fully accommodate all of the bits of the bit-field itself.
15302 This simple rule makes it obvious why GCC allocates 8 bytes for
15303 each object of the structure type shown above. When looking
15304 for a place to allocate the "containing object" for `field2',
15305 the compiler simply tries to allocate a 64-bit "containing
15306 object" at each successive 32-bit boundary (starting at zero)
15307 until it finds a place to allocate that 64- bit field such that
15308 at least 31 contiguous (and previously unallocated) bits remain
15309 within that selected 64 bit field. (As it turns out, for the
15310 example above, the compiler finds it is OK to allocate the
15311 "containing object" 64-bit field at bit-offset zero within the
15312 structure type.)
15314 Here we attempt to work backwards from the limited set of facts
15315 we're given, and we try to deduce from those facts, where GCC
15316 must have believed that the containing object started (within
15317 the structure type). The value we deduce is then used (by the
15318 callers of this routine) to generate DW_AT_location and
15319 DW_AT_bit_offset attributes for fields (both bit-fields and, in
15320 the case of DW_AT_location, regular fields as well). */
15322 /* Figure out the bit-distance from the start of the structure to
15323 the "deepest" bit of the bit-field. */
15324 deepest_bitpos = bitpos_int + field_size_in_bits;
15326 /* This is the tricky part. Use some fancy footwork to deduce
15327 where the lowest addressed bit of the containing object must
15328 be. */
15329 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15331 /* Round up to type_align by default. This works best for
15332 bitfields. */
15333 object_offset_in_bits
15334 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15336 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15338 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15340 /* Round up to decl_align instead. */
15341 object_offset_in_bits
15342 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15345 else
15346 object_offset_in_bits = bitpos_int;
15348 object_offset_in_bytes
15349 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15350 return object_offset_in_bytes.to_shwi ();
15353 /* The following routines define various Dwarf attributes and any data
15354 associated with them. */
15356 /* Add a location description attribute value to a DIE.
15358 This emits location attributes suitable for whole variables and
15359 whole parameters. Note that the location attributes for struct fields are
15360 generated by the routine `data_member_location_attribute' below. */
15362 static inline void
15363 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15364 dw_loc_list_ref descr)
15366 if (descr == 0)
15367 return;
15368 if (single_element_loc_list_p (descr))
15369 add_AT_loc (die, attr_kind, descr->expr);
15370 else
15371 add_AT_loc_list (die, attr_kind, descr);
15374 /* Add DW_AT_accessibility attribute to DIE if needed. */
15376 static void
15377 add_accessibility_attribute (dw_die_ref die, tree decl)
15379 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15380 children, otherwise the default is DW_ACCESS_public. In DWARF2
15381 the default has always been DW_ACCESS_public. */
15382 if (TREE_PROTECTED (decl))
15383 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15384 else if (TREE_PRIVATE (decl))
15386 if (dwarf_version == 2
15387 || die->die_parent == NULL
15388 || die->die_parent->die_tag != DW_TAG_class_type)
15389 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15391 else if (dwarf_version > 2
15392 && die->die_parent
15393 && die->die_parent->die_tag == DW_TAG_class_type)
15394 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15397 /* Attach the specialized form of location attribute used for data members of
15398 struct and union types. In the special case of a FIELD_DECL node which
15399 represents a bit-field, the "offset" part of this special location
15400 descriptor must indicate the distance in bytes from the lowest-addressed
15401 byte of the containing struct or union type to the lowest-addressed byte of
15402 the "containing object" for the bit-field. (See the `field_byte_offset'
15403 function above).
15405 For any given bit-field, the "containing object" is a hypothetical object
15406 (of some integral or enum type) within which the given bit-field lives. The
15407 type of this hypothetical "containing object" is always the same as the
15408 declared type of the individual bit-field itself (for GCC anyway... the
15409 DWARF spec doesn't actually mandate this). Note that it is the size (in
15410 bytes) of the hypothetical "containing object" which will be given in the
15411 DW_AT_byte_size attribute for this bit-field. (See the
15412 `byte_size_attribute' function below.) It is also used when calculating the
15413 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15414 function below.) */
15416 static void
15417 add_data_member_location_attribute (dw_die_ref die, tree decl)
15419 HOST_WIDE_INT offset;
15420 dw_loc_descr_ref loc_descr = 0;
15422 if (TREE_CODE (decl) == TREE_BINFO)
15424 /* We're working on the TAG_inheritance for a base class. */
15425 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15427 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15428 aren't at a fixed offset from all (sub)objects of the same
15429 type. We need to extract the appropriate offset from our
15430 vtable. The following dwarf expression means
15432 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15434 This is specific to the V3 ABI, of course. */
15436 dw_loc_descr_ref tmp;
15438 /* Make a copy of the object address. */
15439 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15440 add_loc_descr (&loc_descr, tmp);
15442 /* Extract the vtable address. */
15443 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15444 add_loc_descr (&loc_descr, tmp);
15446 /* Calculate the address of the offset. */
15447 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15448 gcc_assert (offset < 0);
15450 tmp = int_loc_descriptor (-offset);
15451 add_loc_descr (&loc_descr, tmp);
15452 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15453 add_loc_descr (&loc_descr, tmp);
15455 /* Extract the offset. */
15456 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15457 add_loc_descr (&loc_descr, tmp);
15459 /* Add it to the object address. */
15460 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15461 add_loc_descr (&loc_descr, tmp);
15463 else
15464 offset = tree_to_shwi (BINFO_OFFSET (decl));
15466 else
15467 offset = field_byte_offset (decl);
15469 if (! loc_descr)
15471 if (dwarf_version > 2)
15473 /* Don't need to output a location expression, just the constant. */
15474 if (offset < 0)
15475 add_AT_int (die, DW_AT_data_member_location, offset);
15476 else
15477 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15478 return;
15480 else
15482 enum dwarf_location_atom op;
15484 /* The DWARF2 standard says that we should assume that the structure
15485 address is already on the stack, so we can specify a structure
15486 field address by using DW_OP_plus_uconst. */
15487 op = DW_OP_plus_uconst;
15488 loc_descr = new_loc_descr (op, offset, 0);
15492 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15495 /* Writes integer values to dw_vec_const array. */
15497 static void
15498 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15500 while (size != 0)
15502 *dest++ = val & 0xff;
15503 val >>= 8;
15504 --size;
15508 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15510 static HOST_WIDE_INT
15511 extract_int (const unsigned char *src, unsigned int size)
15513 HOST_WIDE_INT val = 0;
15515 src += size;
15516 while (size != 0)
15518 val <<= 8;
15519 val |= *--src & 0xff;
15520 --size;
15522 return val;
15525 /* Writes wide_int values to dw_vec_const array. */
15527 static void
15528 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15530 int i;
15532 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15534 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15535 return;
15538 /* We'd have to extend this code to support odd sizes. */
15539 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15541 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15543 if (WORDS_BIG_ENDIAN)
15544 for (i = n - 1; i >= 0; i--)
15546 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15547 dest += sizeof (HOST_WIDE_INT);
15549 else
15550 for (i = 0; i < n; i++)
15552 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15553 dest += sizeof (HOST_WIDE_INT);
15557 /* Writes floating point values to dw_vec_const array. */
15559 static void
15560 insert_float (const_rtx rtl, unsigned char *array)
15562 long val[4];
15563 int i;
15565 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
15567 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15568 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15570 insert_int (val[i], 4, array);
15571 array += 4;
15575 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15576 does not have a "location" either in memory or in a register. These
15577 things can arise in GNU C when a constant is passed as an actual parameter
15578 to an inlined function. They can also arise in C++ where declared
15579 constants do not necessarily get memory "homes". */
15581 static bool
15582 add_const_value_attribute (dw_die_ref die, rtx rtl)
15584 switch (GET_CODE (rtl))
15586 case CONST_INT:
15588 HOST_WIDE_INT val = INTVAL (rtl);
15590 if (val < 0)
15591 add_AT_int (die, DW_AT_const_value, val);
15592 else
15593 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15595 return true;
15597 case CONST_WIDE_INT:
15599 wide_int w1 = std::make_pair (rtl, MAX_MODE_INT);
15600 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
15601 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
15602 wide_int w = wi::zext (w1, prec);
15603 add_AT_wide (die, DW_AT_const_value, w);
15605 return true;
15607 case CONST_DOUBLE:
15608 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15609 floating-point constant. A CONST_DOUBLE is used whenever the
15610 constant requires more than one word in order to be adequately
15611 represented. */
15613 machine_mode mode = GET_MODE (rtl);
15615 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15616 add_AT_double (die, DW_AT_const_value,
15617 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15618 else
15620 unsigned int length = GET_MODE_SIZE (mode);
15621 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15623 insert_float (rtl, array);
15624 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15627 return true;
15629 case CONST_VECTOR:
15631 machine_mode mode = GET_MODE (rtl);
15632 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15633 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15634 unsigned char *array
15635 = ggc_vec_alloc<unsigned char> (length * elt_size);
15636 unsigned int i;
15637 unsigned char *p;
15638 machine_mode imode = GET_MODE_INNER (mode);
15640 switch (GET_MODE_CLASS (mode))
15642 case MODE_VECTOR_INT:
15643 for (i = 0, p = array; i < length; i++, p += elt_size)
15645 rtx elt = CONST_VECTOR_ELT (rtl, i);
15646 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15648 break;
15650 case MODE_VECTOR_FLOAT:
15651 for (i = 0, p = array; i < length; i++, p += elt_size)
15653 rtx elt = CONST_VECTOR_ELT (rtl, i);
15654 insert_float (elt, p);
15656 break;
15658 default:
15659 gcc_unreachable ();
15662 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15664 return true;
15666 case CONST_STRING:
15667 if (dwarf_version >= 4 || !dwarf_strict)
15669 dw_loc_descr_ref loc_result;
15670 resolve_one_addr (&rtl);
15671 rtl_addr:
15672 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15673 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15674 add_AT_loc (die, DW_AT_location, loc_result);
15675 vec_safe_push (used_rtx_array, rtl);
15676 return true;
15678 return false;
15680 case CONST:
15681 if (CONSTANT_P (XEXP (rtl, 0)))
15682 return add_const_value_attribute (die, XEXP (rtl, 0));
15683 /* FALLTHROUGH */
15684 case SYMBOL_REF:
15685 if (!const_ok_for_output (rtl))
15686 return false;
15687 case LABEL_REF:
15688 if (dwarf_version >= 4 || !dwarf_strict)
15689 goto rtl_addr;
15690 return false;
15692 case PLUS:
15693 /* In cases where an inlined instance of an inline function is passed
15694 the address of an `auto' variable (which is local to the caller) we
15695 can get a situation where the DECL_RTL of the artificial local
15696 variable (for the inlining) which acts as a stand-in for the
15697 corresponding formal parameter (of the inline function) will look
15698 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15699 exactly a compile-time constant expression, but it isn't the address
15700 of the (artificial) local variable either. Rather, it represents the
15701 *value* which the artificial local variable always has during its
15702 lifetime. We currently have no way to represent such quasi-constant
15703 values in Dwarf, so for now we just punt and generate nothing. */
15704 return false;
15706 case HIGH:
15707 case CONST_FIXED:
15708 return false;
15710 case MEM:
15711 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15712 && MEM_READONLY_P (rtl)
15713 && GET_MODE (rtl) == BLKmode)
15715 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15716 return true;
15718 return false;
15720 default:
15721 /* No other kinds of rtx should be possible here. */
15722 gcc_unreachable ();
15724 return false;
15727 /* Determine whether the evaluation of EXPR references any variables
15728 or functions which aren't otherwise used (and therefore may not be
15729 output). */
15730 static tree
15731 reference_to_unused (tree * tp, int * walk_subtrees,
15732 void * data ATTRIBUTE_UNUSED)
15734 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15735 *walk_subtrees = 0;
15737 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15738 && ! TREE_ASM_WRITTEN (*tp))
15739 return *tp;
15740 /* ??? The C++ FE emits debug information for using decls, so
15741 putting gcc_unreachable here falls over. See PR31899. For now
15742 be conservative. */
15743 else if (!symtab->global_info_ready
15744 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15745 return *tp;
15746 else if (TREE_CODE (*tp) == VAR_DECL)
15748 varpool_node *node = varpool_node::get (*tp);
15749 if (!node || !node->definition)
15750 return *tp;
15752 else if (TREE_CODE (*tp) == FUNCTION_DECL
15753 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15755 /* The call graph machinery must have finished analyzing,
15756 optimizing and gimplifying the CU by now.
15757 So if *TP has no call graph node associated
15758 to it, it means *TP will not be emitted. */
15759 if (!cgraph_node::get (*tp))
15760 return *tp;
15762 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15763 return *tp;
15765 return NULL_TREE;
15768 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15769 for use in a later add_const_value_attribute call. */
15771 static rtx
15772 rtl_for_decl_init (tree init, tree type)
15774 rtx rtl = NULL_RTX;
15776 STRIP_NOPS (init);
15778 /* If a variable is initialized with a string constant without embedded
15779 zeros, build CONST_STRING. */
15780 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15782 tree enttype = TREE_TYPE (type);
15783 tree domain = TYPE_DOMAIN (type);
15784 machine_mode mode = TYPE_MODE (enttype);
15786 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15787 && domain
15788 && integer_zerop (TYPE_MIN_VALUE (domain))
15789 && compare_tree_int (TYPE_MAX_VALUE (domain),
15790 TREE_STRING_LENGTH (init) - 1) == 0
15791 && ((size_t) TREE_STRING_LENGTH (init)
15792 == strlen (TREE_STRING_POINTER (init)) + 1))
15794 rtl = gen_rtx_CONST_STRING (VOIDmode,
15795 ggc_strdup (TREE_STRING_POINTER (init)));
15796 rtl = gen_rtx_MEM (BLKmode, rtl);
15797 MEM_READONLY_P (rtl) = 1;
15800 /* Other aggregates, and complex values, could be represented using
15801 CONCAT: FIXME! */
15802 else if (AGGREGATE_TYPE_P (type)
15803 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15804 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15805 || TREE_CODE (type) == COMPLEX_TYPE)
15807 /* Vectors only work if their mode is supported by the target.
15808 FIXME: generic vectors ought to work too. */
15809 else if (TREE_CODE (type) == VECTOR_TYPE
15810 && !VECTOR_MODE_P (TYPE_MODE (type)))
15812 /* If the initializer is something that we know will expand into an
15813 immediate RTL constant, expand it now. We must be careful not to
15814 reference variables which won't be output. */
15815 else if (initializer_constant_valid_p (init, type)
15816 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15818 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15819 possible. */
15820 if (TREE_CODE (type) == VECTOR_TYPE)
15821 switch (TREE_CODE (init))
15823 case VECTOR_CST:
15824 break;
15825 case CONSTRUCTOR:
15826 if (TREE_CONSTANT (init))
15828 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15829 bool constant_p = true;
15830 tree value;
15831 unsigned HOST_WIDE_INT ix;
15833 /* Even when ctor is constant, it might contain non-*_CST
15834 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15835 belong into VECTOR_CST nodes. */
15836 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15837 if (!CONSTANT_CLASS_P (value))
15839 constant_p = false;
15840 break;
15843 if (constant_p)
15845 init = build_vector_from_ctor (type, elts);
15846 break;
15849 /* FALLTHRU */
15851 default:
15852 return NULL;
15855 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15857 /* If expand_expr returns a MEM, it wasn't immediate. */
15858 gcc_assert (!rtl || !MEM_P (rtl));
15861 return rtl;
15864 /* Generate RTL for the variable DECL to represent its location. */
15866 static rtx
15867 rtl_for_decl_location (tree decl)
15869 rtx rtl;
15871 /* Here we have to decide where we are going to say the parameter "lives"
15872 (as far as the debugger is concerned). We only have a couple of
15873 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15875 DECL_RTL normally indicates where the parameter lives during most of the
15876 activation of the function. If optimization is enabled however, this
15877 could be either NULL or else a pseudo-reg. Both of those cases indicate
15878 that the parameter doesn't really live anywhere (as far as the code
15879 generation parts of GCC are concerned) during most of the function's
15880 activation. That will happen (for example) if the parameter is never
15881 referenced within the function.
15883 We could just generate a location descriptor here for all non-NULL
15884 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15885 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15886 where DECL_RTL is NULL or is a pseudo-reg.
15888 Note however that we can only get away with using DECL_INCOMING_RTL as
15889 a backup substitute for DECL_RTL in certain limited cases. In cases
15890 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15891 we can be sure that the parameter was passed using the same type as it is
15892 declared to have within the function, and that its DECL_INCOMING_RTL
15893 points us to a place where a value of that type is passed.
15895 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15896 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15897 because in these cases DECL_INCOMING_RTL points us to a value of some
15898 type which is *different* from the type of the parameter itself. Thus,
15899 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15900 such cases, the debugger would end up (for example) trying to fetch a
15901 `float' from a place which actually contains the first part of a
15902 `double'. That would lead to really incorrect and confusing
15903 output at debug-time.
15905 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15906 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15907 are a couple of exceptions however. On little-endian machines we can
15908 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15909 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15910 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15911 when (on a little-endian machine) a non-prototyped function has a
15912 parameter declared to be of type `short' or `char'. In such cases,
15913 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15914 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15915 passed `int' value. If the debugger then uses that address to fetch
15916 a `short' or a `char' (on a little-endian machine) the result will be
15917 the correct data, so we allow for such exceptional cases below.
15919 Note that our goal here is to describe the place where the given formal
15920 parameter lives during most of the function's activation (i.e. between the
15921 end of the prologue and the start of the epilogue). We'll do that as best
15922 as we can. Note however that if the given formal parameter is modified
15923 sometime during the execution of the function, then a stack backtrace (at
15924 debug-time) will show the function as having been called with the *new*
15925 value rather than the value which was originally passed in. This happens
15926 rarely enough that it is not a major problem, but it *is* a problem, and
15927 I'd like to fix it.
15929 A future version of dwarf2out.c may generate two additional attributes for
15930 any given DW_TAG_formal_parameter DIE which will describe the "passed
15931 type" and the "passed location" for the given formal parameter in addition
15932 to the attributes we now generate to indicate the "declared type" and the
15933 "active location" for each parameter. This additional set of attributes
15934 could be used by debuggers for stack backtraces. Separately, note that
15935 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15936 This happens (for example) for inlined-instances of inline function formal
15937 parameters which are never referenced. This really shouldn't be
15938 happening. All PARM_DECL nodes should get valid non-NULL
15939 DECL_INCOMING_RTL values. FIXME. */
15941 /* Use DECL_RTL as the "location" unless we find something better. */
15942 rtl = DECL_RTL_IF_SET (decl);
15944 /* When generating abstract instances, ignore everything except
15945 constants, symbols living in memory, and symbols living in
15946 fixed registers. */
15947 if (! reload_completed)
15949 if (rtl
15950 && (CONSTANT_P (rtl)
15951 || (MEM_P (rtl)
15952 && CONSTANT_P (XEXP (rtl, 0)))
15953 || (REG_P (rtl)
15954 && TREE_CODE (decl) == VAR_DECL
15955 && TREE_STATIC (decl))))
15957 rtl = targetm.delegitimize_address (rtl);
15958 return rtl;
15960 rtl = NULL_RTX;
15962 else if (TREE_CODE (decl) == PARM_DECL)
15964 if (rtl == NULL_RTX
15965 || is_pseudo_reg (rtl)
15966 || (MEM_P (rtl)
15967 && is_pseudo_reg (XEXP (rtl, 0))
15968 && DECL_INCOMING_RTL (decl)
15969 && MEM_P (DECL_INCOMING_RTL (decl))
15970 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15972 tree declared_type = TREE_TYPE (decl);
15973 tree passed_type = DECL_ARG_TYPE (decl);
15974 machine_mode dmode = TYPE_MODE (declared_type);
15975 machine_mode pmode = TYPE_MODE (passed_type);
15977 /* This decl represents a formal parameter which was optimized out.
15978 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15979 all cases where (rtl == NULL_RTX) just below. */
15980 if (dmode == pmode)
15981 rtl = DECL_INCOMING_RTL (decl);
15982 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15983 && SCALAR_INT_MODE_P (dmode)
15984 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15985 && DECL_INCOMING_RTL (decl))
15987 rtx inc = DECL_INCOMING_RTL (decl);
15988 if (REG_P (inc))
15989 rtl = inc;
15990 else if (MEM_P (inc))
15992 if (BYTES_BIG_ENDIAN)
15993 rtl = adjust_address_nv (inc, dmode,
15994 GET_MODE_SIZE (pmode)
15995 - GET_MODE_SIZE (dmode));
15996 else
15997 rtl = inc;
16002 /* If the parm was passed in registers, but lives on the stack, then
16003 make a big endian correction if the mode of the type of the
16004 parameter is not the same as the mode of the rtl. */
16005 /* ??? This is the same series of checks that are made in dbxout.c before
16006 we reach the big endian correction code there. It isn't clear if all
16007 of these checks are necessary here, but keeping them all is the safe
16008 thing to do. */
16009 else if (MEM_P (rtl)
16010 && XEXP (rtl, 0) != const0_rtx
16011 && ! CONSTANT_P (XEXP (rtl, 0))
16012 /* Not passed in memory. */
16013 && !MEM_P (DECL_INCOMING_RTL (decl))
16014 /* Not passed by invisible reference. */
16015 && (!REG_P (XEXP (rtl, 0))
16016 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16017 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16018 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
16019 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16020 #endif
16022 /* Big endian correction check. */
16023 && BYTES_BIG_ENDIAN
16024 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16025 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16026 < UNITS_PER_WORD))
16028 machine_mode addr_mode = get_address_mode (rtl);
16029 int offset = (UNITS_PER_WORD
16030 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16032 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16033 plus_constant (addr_mode, XEXP (rtl, 0), offset));
16036 else if (TREE_CODE (decl) == VAR_DECL
16037 && rtl
16038 && MEM_P (rtl)
16039 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16040 && BYTES_BIG_ENDIAN)
16042 machine_mode addr_mode = get_address_mode (rtl);
16043 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16044 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16046 /* If a variable is declared "register" yet is smaller than
16047 a register, then if we store the variable to memory, it
16048 looks like we're storing a register-sized value, when in
16049 fact we are not. We need to adjust the offset of the
16050 storage location to reflect the actual value's bytes,
16051 else gdb will not be able to display it. */
16052 if (rsize > dsize)
16053 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16054 plus_constant (addr_mode, XEXP (rtl, 0),
16055 rsize - dsize));
16058 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16059 and will have been substituted directly into all expressions that use it.
16060 C does not have such a concept, but C++ and other languages do. */
16061 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16062 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16064 if (rtl)
16065 rtl = targetm.delegitimize_address (rtl);
16067 /* If we don't look past the constant pool, we risk emitting a
16068 reference to a constant pool entry that isn't referenced from
16069 code, and thus is not emitted. */
16070 if (rtl)
16071 rtl = avoid_constant_pool_reference (rtl);
16073 /* Try harder to get a rtl. If this symbol ends up not being emitted
16074 in the current CU, resolve_addr will remove the expression referencing
16075 it. */
16076 if (rtl == NULL_RTX
16077 && TREE_CODE (decl) == VAR_DECL
16078 && !DECL_EXTERNAL (decl)
16079 && TREE_STATIC (decl)
16080 && DECL_NAME (decl)
16081 && !DECL_HARD_REGISTER (decl)
16082 && DECL_MODE (decl) != VOIDmode)
16084 rtl = make_decl_rtl_for_debug (decl);
16085 if (!MEM_P (rtl)
16086 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16087 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16088 rtl = NULL_RTX;
16091 return rtl;
16094 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
16095 returned. If so, the decl for the COMMON block is returned, and the
16096 value is the offset into the common block for the symbol. */
16098 static tree
16099 fortran_common (tree decl, HOST_WIDE_INT *value)
16101 tree val_expr, cvar;
16102 machine_mode mode;
16103 HOST_WIDE_INT bitsize, bitpos;
16104 tree offset;
16105 int unsignedp, volatilep = 0;
16107 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16108 it does not have a value (the offset into the common area), or if it
16109 is thread local (as opposed to global) then it isn't common, and shouldn't
16110 be handled as such. */
16111 if (TREE_CODE (decl) != VAR_DECL
16112 || !TREE_STATIC (decl)
16113 || !DECL_HAS_VALUE_EXPR_P (decl)
16114 || !is_fortran ())
16115 return NULL_TREE;
16117 val_expr = DECL_VALUE_EXPR (decl);
16118 if (TREE_CODE (val_expr) != COMPONENT_REF)
16119 return NULL_TREE;
16121 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16122 &mode, &unsignedp, &volatilep, true);
16124 if (cvar == NULL_TREE
16125 || TREE_CODE (cvar) != VAR_DECL
16126 || DECL_ARTIFICIAL (cvar)
16127 || !TREE_PUBLIC (cvar))
16128 return NULL_TREE;
16130 *value = 0;
16131 if (offset != NULL)
16133 if (!tree_fits_shwi_p (offset))
16134 return NULL_TREE;
16135 *value = tree_to_shwi (offset);
16137 if (bitpos != 0)
16138 *value += bitpos / BITS_PER_UNIT;
16140 return cvar;
16143 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16144 data attribute for a variable or a parameter. We generate the
16145 DW_AT_const_value attribute only in those cases where the given variable
16146 or parameter does not have a true "location" either in memory or in a
16147 register. This can happen (for example) when a constant is passed as an
16148 actual argument in a call to an inline function. (It's possible that
16149 these things can crop up in other ways also.) Note that one type of
16150 constant value which can be passed into an inlined function is a constant
16151 pointer. This can happen for example if an actual argument in an inlined
16152 function call evaluates to a compile-time constant address.
16154 CACHE_P is true if it is worth caching the location list for DECL,
16155 so that future calls can reuse it rather than regenerate it from scratch.
16156 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
16157 since we will need to refer to them each time the function is inlined. */
16159 static bool
16160 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
16162 rtx rtl;
16163 dw_loc_list_ref list;
16164 var_loc_list *loc_list;
16165 cached_dw_loc_list *cache;
16167 if (early_dwarf)
16168 return false;
16170 if (TREE_CODE (decl) == ERROR_MARK)
16171 return false;
16173 if (get_AT (die, DW_AT_location)
16174 || get_AT (die, DW_AT_const_value))
16175 return true;
16177 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16178 || TREE_CODE (decl) == RESULT_DECL);
16180 /* Try to get some constant RTL for this decl, and use that as the value of
16181 the location. */
16183 rtl = rtl_for_decl_location (decl);
16184 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16185 && add_const_value_attribute (die, rtl))
16186 return true;
16188 /* See if we have single element location list that is equivalent to
16189 a constant value. That way we are better to use add_const_value_attribute
16190 rather than expanding constant value equivalent. */
16191 loc_list = lookup_decl_loc (decl);
16192 if (loc_list
16193 && loc_list->first
16194 && loc_list->first->next == NULL
16195 && NOTE_P (loc_list->first->loc)
16196 && NOTE_VAR_LOCATION (loc_list->first->loc)
16197 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16199 struct var_loc_node *node;
16201 node = loc_list->first;
16202 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16203 if (GET_CODE (rtl) == EXPR_LIST)
16204 rtl = XEXP (rtl, 0);
16205 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16206 && add_const_value_attribute (die, rtl))
16207 return true;
16209 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
16210 list several times. See if we've already cached the contents. */
16211 list = NULL;
16212 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
16213 cache_p = false;
16214 if (cache_p)
16216 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
16217 if (cache)
16218 list = cache->loc_list;
16220 if (list == NULL)
16222 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
16223 NULL);
16224 /* It is usually worth caching this result if the decl is from
16225 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
16226 if (cache_p && list && list->dw_loc_next)
16228 cached_dw_loc_list **slot
16229 = cached_dw_loc_list_table->find_slot_with_hash (decl,
16230 DECL_UID (decl),
16231 INSERT);
16232 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
16233 cache->decl_id = DECL_UID (decl);
16234 cache->loc_list = list;
16235 *slot = cache;
16238 if (list)
16240 add_AT_location_description (die, DW_AT_location, list);
16241 return true;
16243 /* None of that worked, so it must not really have a location;
16244 try adding a constant value attribute from the DECL_INITIAL. */
16245 return tree_add_const_value_attribute_for_decl (die, decl);
16248 /* Helper function for tree_add_const_value_attribute. Natively encode
16249 initializer INIT into an array. Return true if successful. */
16251 static bool
16252 native_encode_initializer (tree init, unsigned char *array, int size)
16254 tree type;
16256 if (init == NULL_TREE)
16257 return false;
16259 STRIP_NOPS (init);
16260 switch (TREE_CODE (init))
16262 case STRING_CST:
16263 type = TREE_TYPE (init);
16264 if (TREE_CODE (type) == ARRAY_TYPE)
16266 tree enttype = TREE_TYPE (type);
16267 machine_mode mode = TYPE_MODE (enttype);
16269 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16270 return false;
16271 if (int_size_in_bytes (type) != size)
16272 return false;
16273 if (size > TREE_STRING_LENGTH (init))
16275 memcpy (array, TREE_STRING_POINTER (init),
16276 TREE_STRING_LENGTH (init));
16277 memset (array + TREE_STRING_LENGTH (init),
16278 '\0', size - TREE_STRING_LENGTH (init));
16280 else
16281 memcpy (array, TREE_STRING_POINTER (init), size);
16282 return true;
16284 return false;
16285 case CONSTRUCTOR:
16286 type = TREE_TYPE (init);
16287 if (int_size_in_bytes (type) != size)
16288 return false;
16289 if (TREE_CODE (type) == ARRAY_TYPE)
16291 HOST_WIDE_INT min_index;
16292 unsigned HOST_WIDE_INT cnt;
16293 int curpos = 0, fieldsize;
16294 constructor_elt *ce;
16296 if (TYPE_DOMAIN (type) == NULL_TREE
16297 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
16298 return false;
16300 fieldsize = int_size_in_bytes (TREE_TYPE (type));
16301 if (fieldsize <= 0)
16302 return false;
16304 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
16305 memset (array, '\0', size);
16306 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16308 tree val = ce->value;
16309 tree index = ce->index;
16310 int pos = curpos;
16311 if (index && TREE_CODE (index) == RANGE_EXPR)
16312 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
16313 * fieldsize;
16314 else if (index)
16315 pos = (tree_to_shwi (index) - min_index) * fieldsize;
16317 if (val)
16319 STRIP_NOPS (val);
16320 if (!native_encode_initializer (val, array + pos, fieldsize))
16321 return false;
16323 curpos = pos + fieldsize;
16324 if (index && TREE_CODE (index) == RANGE_EXPR)
16326 int count = tree_to_shwi (TREE_OPERAND (index, 1))
16327 - tree_to_shwi (TREE_OPERAND (index, 0));
16328 while (count-- > 0)
16330 if (val)
16331 memcpy (array + curpos, array + pos, fieldsize);
16332 curpos += fieldsize;
16335 gcc_assert (curpos <= size);
16337 return true;
16339 else if (TREE_CODE (type) == RECORD_TYPE
16340 || TREE_CODE (type) == UNION_TYPE)
16342 tree field = NULL_TREE;
16343 unsigned HOST_WIDE_INT cnt;
16344 constructor_elt *ce;
16346 if (int_size_in_bytes (type) != size)
16347 return false;
16349 if (TREE_CODE (type) == RECORD_TYPE)
16350 field = TYPE_FIELDS (type);
16352 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16354 tree val = ce->value;
16355 int pos, fieldsize;
16357 if (ce->index != 0)
16358 field = ce->index;
16360 if (val)
16361 STRIP_NOPS (val);
16363 if (field == NULL_TREE || DECL_BIT_FIELD (field))
16364 return false;
16366 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16367 && TYPE_DOMAIN (TREE_TYPE (field))
16368 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16369 return false;
16370 else if (DECL_SIZE_UNIT (field) == NULL_TREE
16371 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16372 return false;
16373 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16374 pos = int_byte_position (field);
16375 gcc_assert (pos + fieldsize <= size);
16376 if (val
16377 && !native_encode_initializer (val, array + pos, fieldsize))
16378 return false;
16380 return true;
16382 return false;
16383 case VIEW_CONVERT_EXPR:
16384 case NON_LVALUE_EXPR:
16385 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16386 default:
16387 return native_encode_expr (init, array, size) == size;
16391 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16392 attribute is the const value T. */
16394 static bool
16395 tree_add_const_value_attribute (dw_die_ref die, tree t)
16397 tree init;
16398 tree type = TREE_TYPE (t);
16399 rtx rtl;
16401 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16402 return false;
16404 init = t;
16405 gcc_assert (!DECL_P (init));
16407 rtl = rtl_for_decl_init (init, type);
16408 if (rtl)
16409 return add_const_value_attribute (die, rtl);
16410 /* If the host and target are sane, try harder. */
16411 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16412 && initializer_constant_valid_p (init, type))
16414 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16415 if (size > 0 && (int) size == size)
16417 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16419 if (native_encode_initializer (init, array, size))
16421 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16422 return true;
16424 ggc_free (array);
16427 return false;
16430 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16431 attribute is the const value of T, where T is an integral constant
16432 variable with static storage duration
16433 (so it can't be a PARM_DECL or a RESULT_DECL). */
16435 static bool
16436 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16439 if (!decl
16440 || (TREE_CODE (decl) != VAR_DECL
16441 && TREE_CODE (decl) != CONST_DECL)
16442 || (TREE_CODE (decl) == VAR_DECL
16443 && !TREE_STATIC (decl)))
16444 return false;
16446 if (TREE_READONLY (decl)
16447 && ! TREE_THIS_VOLATILE (decl)
16448 && DECL_INITIAL (decl))
16449 /* OK */;
16450 else
16451 return false;
16453 /* Don't add DW_AT_const_value if abstract origin already has one. */
16454 if (get_AT (var_die, DW_AT_const_value))
16455 return false;
16457 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16460 /* Convert the CFI instructions for the current function into a
16461 location list. This is used for DW_AT_frame_base when we targeting
16462 a dwarf2 consumer that does not support the dwarf3
16463 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16464 expressions. */
16466 static dw_loc_list_ref
16467 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16469 int ix;
16470 dw_fde_ref fde;
16471 dw_loc_list_ref list, *list_tail;
16472 dw_cfi_ref cfi;
16473 dw_cfa_location last_cfa, next_cfa;
16474 const char *start_label, *last_label, *section;
16475 dw_cfa_location remember;
16477 fde = cfun->fde;
16478 gcc_assert (fde != NULL);
16480 section = secname_for_decl (current_function_decl);
16481 list_tail = &list;
16482 list = NULL;
16484 memset (&next_cfa, 0, sizeof (next_cfa));
16485 next_cfa.reg = INVALID_REGNUM;
16486 remember = next_cfa;
16488 start_label = fde->dw_fde_begin;
16490 /* ??? Bald assumption that the CIE opcode list does not contain
16491 advance opcodes. */
16492 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16493 lookup_cfa_1 (cfi, &next_cfa, &remember);
16495 last_cfa = next_cfa;
16496 last_label = start_label;
16498 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16500 /* If the first partition contained no CFI adjustments, the
16501 CIE opcodes apply to the whole first partition. */
16502 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16503 fde->dw_fde_begin, fde->dw_fde_end, section);
16504 list_tail =&(*list_tail)->dw_loc_next;
16505 start_label = last_label = fde->dw_fde_second_begin;
16508 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16510 switch (cfi->dw_cfi_opc)
16512 case DW_CFA_set_loc:
16513 case DW_CFA_advance_loc1:
16514 case DW_CFA_advance_loc2:
16515 case DW_CFA_advance_loc4:
16516 if (!cfa_equal_p (&last_cfa, &next_cfa))
16518 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16519 start_label, last_label, section);
16521 list_tail = &(*list_tail)->dw_loc_next;
16522 last_cfa = next_cfa;
16523 start_label = last_label;
16525 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16526 break;
16528 case DW_CFA_advance_loc:
16529 /* The encoding is complex enough that we should never emit this. */
16530 gcc_unreachable ();
16532 default:
16533 lookup_cfa_1 (cfi, &next_cfa, &remember);
16534 break;
16536 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16538 if (!cfa_equal_p (&last_cfa, &next_cfa))
16540 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16541 start_label, last_label, section);
16543 list_tail = &(*list_tail)->dw_loc_next;
16544 last_cfa = next_cfa;
16545 start_label = last_label;
16547 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16548 start_label, fde->dw_fde_end, section);
16549 list_tail = &(*list_tail)->dw_loc_next;
16550 start_label = last_label = fde->dw_fde_second_begin;
16554 if (!cfa_equal_p (&last_cfa, &next_cfa))
16556 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16557 start_label, last_label, section);
16558 list_tail = &(*list_tail)->dw_loc_next;
16559 start_label = last_label;
16562 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16563 start_label,
16564 fde->dw_fde_second_begin
16565 ? fde->dw_fde_second_end : fde->dw_fde_end,
16566 section);
16568 if (list && list->dw_loc_next)
16569 gen_llsym (list);
16571 return list;
16574 /* Compute a displacement from the "steady-state frame pointer" to the
16575 frame base (often the same as the CFA), and store it in
16576 frame_pointer_fb_offset. OFFSET is added to the displacement
16577 before the latter is negated. */
16579 static void
16580 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16582 rtx reg, elim;
16584 #ifdef FRAME_POINTER_CFA_OFFSET
16585 reg = frame_pointer_rtx;
16586 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16587 #else
16588 reg = arg_pointer_rtx;
16589 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16590 #endif
16592 elim = (ira_use_lra_p
16593 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16594 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16595 if (GET_CODE (elim) == PLUS)
16597 offset += INTVAL (XEXP (elim, 1));
16598 elim = XEXP (elim, 0);
16601 frame_pointer_fb_offset = -offset;
16603 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16604 in which to eliminate. This is because it's stack pointer isn't
16605 directly accessible as a register within the ISA. To work around
16606 this, assume that while we cannot provide a proper value for
16607 frame_pointer_fb_offset, we won't need one either. */
16608 frame_pointer_fb_offset_valid
16609 = ((SUPPORTS_STACK_ALIGNMENT
16610 && (elim == hard_frame_pointer_rtx
16611 || elim == stack_pointer_rtx))
16612 || elim == (frame_pointer_needed
16613 ? hard_frame_pointer_rtx
16614 : stack_pointer_rtx));
16617 /* Generate a DW_AT_name attribute given some string value to be included as
16618 the value of the attribute. */
16620 static void
16621 add_name_attribute (dw_die_ref die, const char *name_string)
16623 if (name_string != NULL && *name_string != 0)
16625 if (demangle_name_func)
16626 name_string = (*demangle_name_func) (name_string);
16628 add_AT_string (die, DW_AT_name, name_string);
16632 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16633 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16634 of TYPE accordingly.
16636 ??? This is a temporary measure until after we're able to generate
16637 regular DWARF for the complex Ada type system. */
16639 static void
16640 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16641 dw_die_ref context_die)
16643 tree dtype;
16644 dw_die_ref dtype_die;
16646 if (!lang_hooks.types.descriptive_type)
16647 return;
16649 dtype = lang_hooks.types.descriptive_type (type);
16650 if (!dtype)
16651 return;
16653 dtype_die = lookup_type_die (dtype);
16654 if (!dtype_die)
16656 gen_type_die (dtype, context_die);
16657 dtype_die = lookup_type_die (dtype);
16658 gcc_assert (dtype_die);
16661 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16664 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16666 static const char *
16667 comp_dir_string (void)
16669 const char *wd;
16670 char *wd1;
16671 static const char *cached_wd = NULL;
16673 if (cached_wd != NULL)
16674 return cached_wd;
16676 wd = get_src_pwd ();
16677 if (wd == NULL)
16678 return NULL;
16680 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16682 int wdlen;
16684 wdlen = strlen (wd);
16685 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16686 strcpy (wd1, wd);
16687 wd1 [wdlen] = DIR_SEPARATOR;
16688 wd1 [wdlen + 1] = 0;
16689 wd = wd1;
16692 cached_wd = remap_debug_filename (wd);
16693 return cached_wd;
16696 /* Generate a DW_AT_comp_dir attribute for DIE. */
16698 static void
16699 add_comp_dir_attribute (dw_die_ref die)
16701 const char * wd = comp_dir_string ();
16702 if (wd != NULL)
16703 add_AT_string (die, DW_AT_comp_dir, wd);
16706 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
16707 pointer computation, ...), output a representation for that bound according
16708 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
16709 loc_list_from_tree for the meaning of CONTEXT. */
16711 static void
16712 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
16713 int forms, const struct loc_descr_context *context)
16715 dw_die_ref ctx, decl_die;
16716 dw_loc_list_ref list;
16718 bool strip_conversions = true;
16720 while (strip_conversions)
16721 switch (TREE_CODE (value))
16723 case ERROR_MARK:
16724 case SAVE_EXPR:
16725 return;
16727 CASE_CONVERT:
16728 case VIEW_CONVERT_EXPR:
16729 value = TREE_OPERAND (value, 0);
16730 break;
16732 default:
16733 strip_conversions = false;
16734 break;
16737 /* If possible and permitted, output the attribute as a constant. */
16738 if ((forms & dw_scalar_form_constant) != 0
16739 && TREE_CODE (value) == INTEGER_CST)
16741 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
16743 /* If HOST_WIDE_INT is big enough then represent the bound as
16744 a constant value. We need to choose a form based on
16745 whether the type is signed or unsigned. We cannot just
16746 call add_AT_unsigned if the value itself is positive
16747 (add_AT_unsigned might add the unsigned value encoded as
16748 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16749 bounds type and then sign extend any unsigned values found
16750 for signed types. This is needed only for
16751 DW_AT_{lower,upper}_bound, since for most other attributes,
16752 consumers will treat DW_FORM_data[1248] as unsigned values,
16753 regardless of the underlying type. */
16754 if (prec <= HOST_BITS_PER_WIDE_INT
16755 || tree_fits_uhwi_p (value))
16757 if (TYPE_UNSIGNED (TREE_TYPE (value)))
16758 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
16759 else
16760 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
16762 else
16763 /* Otherwise represent the bound as an unsigned value with
16764 the precision of its type. The precision and signedness
16765 of the type will be necessary to re-interpret it
16766 unambiguously. */
16767 add_AT_wide (die, attr, value);
16768 return;
16771 /* Otherwise, if it's possible and permitted too, output a reference to
16772 another DIE. */
16773 if ((forms & dw_scalar_form_reference) != 0)
16775 tree decl = NULL_TREE;
16777 /* Some type attributes reference an outer type. For instance, the upper
16778 bound of an array may reference an embedding record (this happens in
16779 Ada). */
16780 if (TREE_CODE (value) == COMPONENT_REF
16781 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
16782 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
16783 decl = TREE_OPERAND (value, 1);
16785 else if (TREE_CODE (value) == VAR_DECL
16786 || TREE_CODE (value) == PARM_DECL
16787 || TREE_CODE (value) == RESULT_DECL)
16788 decl = value;
16790 if (decl != NULL_TREE)
16792 dw_die_ref decl_die = lookup_decl_die (decl);
16794 /* ??? Can this happen, or should the variable have been bound
16795 first? Probably it can, since I imagine that we try to create
16796 the types of parameters in the order in which they exist in
16797 the list, and won't have created a forward reference to a
16798 later parameter. */
16799 if (decl_die != NULL)
16801 add_AT_die_ref (die, attr, decl_die);
16802 return;
16807 /* Last chance: try to create a stack operation procedure to evaluate the
16808 value. Do nothing if even that is not possible or permitted. */
16809 if ((forms & dw_scalar_form_exprloc) == 0)
16810 return;
16812 list = loc_list_from_tree (value, 2, context);
16813 if (list == NULL || single_element_loc_list_p (list))
16815 /* If this attribute is not a reference nor constant, it is
16816 a DWARF expression rather than location description. For that
16817 loc_list_from_tree (value, 0, &context) is needed. */
16818 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
16819 if (list2 && single_element_loc_list_p (list2))
16821 add_AT_loc (die, attr, list2->expr);
16822 return;
16826 /* If that failed to give a single element location list, fall back to
16827 outputting this as a reference... still if permitted. */
16828 if (list == NULL || (forms & dw_scalar_form_reference) == 0)
16829 return;
16831 if (current_function_decl == 0)
16832 ctx = comp_unit_die ();
16833 else
16834 ctx = lookup_decl_die (current_function_decl);
16836 decl_die = new_die (DW_TAG_variable, ctx, value);
16837 add_AT_flag (decl_die, DW_AT_artificial, 1);
16838 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, ctx);
16839 add_AT_location_description (decl_die, DW_AT_location, list);
16840 add_AT_die_ref (die, attr, decl_die);
16843 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16844 default. */
16846 static int
16847 lower_bound_default (void)
16849 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16851 case DW_LANG_C:
16852 case DW_LANG_C89:
16853 case DW_LANG_C99:
16854 case DW_LANG_C11:
16855 case DW_LANG_C_plus_plus:
16856 case DW_LANG_C_plus_plus_11:
16857 case DW_LANG_C_plus_plus_14:
16858 case DW_LANG_ObjC:
16859 case DW_LANG_ObjC_plus_plus:
16860 case DW_LANG_Java:
16861 return 0;
16862 case DW_LANG_Fortran77:
16863 case DW_LANG_Fortran90:
16864 case DW_LANG_Fortran95:
16865 case DW_LANG_Fortran03:
16866 case DW_LANG_Fortran08:
16867 return 1;
16868 case DW_LANG_UPC:
16869 case DW_LANG_D:
16870 case DW_LANG_Python:
16871 return dwarf_version >= 4 ? 0 : -1;
16872 case DW_LANG_Ada95:
16873 case DW_LANG_Ada83:
16874 case DW_LANG_Cobol74:
16875 case DW_LANG_Cobol85:
16876 case DW_LANG_Pascal83:
16877 case DW_LANG_Modula2:
16878 case DW_LANG_PLI:
16879 return dwarf_version >= 4 ? 1 : -1;
16880 default:
16881 return -1;
16885 /* Given a tree node describing an array bound (either lower or upper) output
16886 a representation for that bound. */
16888 static void
16889 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
16890 tree bound, const struct loc_descr_context *context)
16892 int dflt;
16894 while (1)
16895 switch (TREE_CODE (bound))
16897 /* Strip all conversions. */
16898 CASE_CONVERT:
16899 case VIEW_CONVERT_EXPR:
16900 bound = TREE_OPERAND (bound, 0);
16901 break;
16903 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
16904 are even omitted when they are the default. */
16905 case INTEGER_CST:
16906 /* If the value for this bound is the default one, we can even omit the
16907 attribute. */
16908 if (bound_attr == DW_AT_lower_bound
16909 && tree_fits_shwi_p (bound)
16910 && (dflt = lower_bound_default ()) != -1
16911 && tree_to_shwi (bound) == dflt)
16912 return;
16914 /* FALLTHRU */
16916 default:
16917 add_scalar_info (subrange_die, bound_attr, bound,
16918 dw_scalar_form_constant
16919 | dw_scalar_form_exprloc
16920 | dw_scalar_form_reference,
16921 context);
16922 return;
16926 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16927 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16928 Note that the block of subscript information for an array type also
16929 includes information about the element type of the given array type.
16931 This function reuses previously set type and bound information if
16932 available. */
16934 static void
16935 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16937 unsigned dimension_number;
16938 tree lower, upper;
16939 dw_die_ref child = type_die->die_child;
16941 for (dimension_number = 0;
16942 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16943 type = TREE_TYPE (type), dimension_number++)
16945 tree domain = TYPE_DOMAIN (type);
16947 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16948 break;
16950 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16951 and (in GNU C only) variable bounds. Handle all three forms
16952 here. */
16954 /* Find and reuse a previously generated DW_TAG_subrange_type if
16955 available.
16957 For multi-dimensional arrays, as we iterate through the
16958 various dimensions in the enclosing for loop above, we also
16959 iterate through the DIE children and pick at each
16960 DW_TAG_subrange_type previously generated (if available).
16961 Each child DW_TAG_subrange_type DIE describes the range of
16962 the current dimension. At this point we should have as many
16963 DW_TAG_subrange_type's as we have dimensions in the
16964 array. */
16965 dw_die_ref subrange_die = NULL;
16966 if (child)
16967 while (1)
16969 child = child->die_sib;
16970 if (child->die_tag == DW_TAG_subrange_type)
16971 subrange_die = child;
16972 if (child == type_die->die_child)
16974 /* If we wrapped around, stop looking next time. */
16975 child = NULL;
16976 break;
16978 if (child->die_tag == DW_TAG_subrange_type)
16979 break;
16981 if (!subrange_die)
16982 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16984 if (domain)
16986 /* We have an array type with specified bounds. */
16987 lower = TYPE_MIN_VALUE (domain);
16988 upper = TYPE_MAX_VALUE (domain);
16990 /* Define the index type. */
16991 if (TREE_TYPE (domain)
16992 && !get_AT (subrange_die, DW_AT_type))
16994 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16995 TREE_TYPE field. We can't emit debug info for this
16996 because it is an unnamed integral type. */
16997 if (TREE_CODE (domain) == INTEGER_TYPE
16998 && TYPE_NAME (domain) == NULL_TREE
16999 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17000 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17002 else
17003 add_type_attribute (subrange_die, TREE_TYPE (domain),
17004 TYPE_UNQUALIFIED, type_die);
17007 /* ??? If upper is NULL, the array has unspecified length,
17008 but it does have a lower bound. This happens with Fortran
17009 dimension arr(N:*)
17010 Since the debugger is definitely going to need to know N
17011 to produce useful results, go ahead and output the lower
17012 bound solo, and hope the debugger can cope. */
17014 if (!get_AT (subrange_die, DW_AT_lower_bound))
17015 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
17016 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
17017 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
17020 /* Otherwise we have an array type with an unspecified length. The
17021 DWARF-2 spec does not say how to handle this; let's just leave out the
17022 bounds. */
17026 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
17028 static void
17029 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17031 dw_die_ref decl_die;
17032 HOST_WIDE_INT size;
17034 switch (TREE_CODE (tree_node))
17036 case ERROR_MARK:
17037 size = 0;
17038 break;
17039 case ENUMERAL_TYPE:
17040 case RECORD_TYPE:
17041 case UNION_TYPE:
17042 case QUAL_UNION_TYPE:
17043 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
17044 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
17046 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
17047 return;
17049 size = int_size_in_bytes (tree_node);
17050 break;
17051 case FIELD_DECL:
17052 /* For a data member of a struct or union, the DW_AT_byte_size is
17053 generally given as the number of bytes normally allocated for an
17054 object of the *declared* type of the member itself. This is true
17055 even for bit-fields. */
17056 size = int_size_in_bytes (field_type (tree_node));
17057 break;
17058 default:
17059 gcc_unreachable ();
17062 /* Note that `size' might be -1 when we get to this point. If it is, that
17063 indicates that the byte size of the entity in question is variable. We
17064 have no good way of expressing this fact in Dwarf at the present time,
17065 when location description was not used by the caller code instead. */
17066 if (size >= 0)
17067 add_AT_unsigned (die, DW_AT_byte_size, size);
17070 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17071 which specifies the distance in bits from the highest order bit of the
17072 "containing object" for the bit-field to the highest order bit of the
17073 bit-field itself.
17075 For any given bit-field, the "containing object" is a hypothetical object
17076 (of some integral or enum type) within which the given bit-field lives. The
17077 type of this hypothetical "containing object" is always the same as the
17078 declared type of the individual bit-field itself. The determination of the
17079 exact location of the "containing object" for a bit-field is rather
17080 complicated. It's handled by the `field_byte_offset' function (above).
17082 Note that it is the size (in bytes) of the hypothetical "containing object"
17083 which will be given in the DW_AT_byte_size attribute for this bit-field.
17084 (See `byte_size_attribute' above). */
17086 static inline void
17087 add_bit_offset_attribute (dw_die_ref die, tree decl)
17089 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17090 tree type = DECL_BIT_FIELD_TYPE (decl);
17091 HOST_WIDE_INT bitpos_int;
17092 HOST_WIDE_INT highest_order_object_bit_offset;
17093 HOST_WIDE_INT highest_order_field_bit_offset;
17094 HOST_WIDE_INT bit_offset;
17096 /* Must be a field and a bit field. */
17097 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17099 /* We can't yet handle bit-fields whose offsets are variable, so if we
17100 encounter such things, just return without generating any attribute
17101 whatsoever. Likewise for variable or too large size. */
17102 if (! tree_fits_shwi_p (bit_position (decl))
17103 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
17104 return;
17106 bitpos_int = int_bit_position (decl);
17108 /* Note that the bit offset is always the distance (in bits) from the
17109 highest-order bit of the "containing object" to the highest-order bit of
17110 the bit-field itself. Since the "high-order end" of any object or field
17111 is different on big-endian and little-endian machines, the computation
17112 below must take account of these differences. */
17113 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17114 highest_order_field_bit_offset = bitpos_int;
17116 if (! BYTES_BIG_ENDIAN)
17118 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
17119 highest_order_object_bit_offset += simple_type_size_in_bits (type);
17122 bit_offset
17123 = (! BYTES_BIG_ENDIAN
17124 ? highest_order_object_bit_offset - highest_order_field_bit_offset
17125 : highest_order_field_bit_offset - highest_order_object_bit_offset);
17127 if (bit_offset < 0)
17128 add_AT_int (die, DW_AT_bit_offset, bit_offset);
17129 else
17130 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
17133 /* For a FIELD_DECL node which represents a bit field, output an attribute
17134 which specifies the length in bits of the given field. */
17136 static inline void
17137 add_bit_size_attribute (dw_die_ref die, tree decl)
17139 /* Must be a field and a bit field. */
17140 gcc_assert (TREE_CODE (decl) == FIELD_DECL
17141 && DECL_BIT_FIELD_TYPE (decl));
17143 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
17144 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
17147 /* If the compiled language is ANSI C, then add a 'prototyped'
17148 attribute, if arg types are given for the parameters of a function. */
17150 static inline void
17151 add_prototyped_attribute (dw_die_ref die, tree func_type)
17153 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17155 case DW_LANG_C:
17156 case DW_LANG_C89:
17157 case DW_LANG_C99:
17158 case DW_LANG_C11:
17159 case DW_LANG_ObjC:
17160 if (prototype_p (func_type))
17161 add_AT_flag (die, DW_AT_prototyped, 1);
17162 break;
17163 default:
17164 break;
17168 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
17169 by looking in either the type declaration or object declaration
17170 equate table. */
17172 static inline dw_die_ref
17173 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17175 dw_die_ref origin_die = NULL;
17177 if (TREE_CODE (origin) != FUNCTION_DECL)
17179 /* We may have gotten separated from the block for the inlined
17180 function, if we're in an exception handler or some such; make
17181 sure that the abstract function has been written out.
17183 Doing this for nested functions is wrong, however; functions are
17184 distinct units, and our context might not even be inline. */
17185 tree fn = origin;
17187 if (TYPE_P (fn))
17188 fn = TYPE_STUB_DECL (fn);
17190 fn = decl_function_context (fn);
17191 if (fn)
17192 dwarf2out_abstract_function (fn);
17195 if (DECL_P (origin))
17196 origin_die = lookup_decl_die (origin);
17197 else if (TYPE_P (origin))
17198 origin_die = lookup_type_die (origin);
17200 /* XXX: Functions that are never lowered don't always have correct block
17201 trees (in the case of java, they simply have no block tree, in some other
17202 languages). For these functions, there is nothing we can really do to
17203 output correct debug info for inlined functions in all cases. Rather
17204 than die, we'll just produce deficient debug info now, in that we will
17205 have variables without a proper abstract origin. In the future, when all
17206 functions are lowered, we should re-add a gcc_assert (origin_die)
17207 here. */
17209 if (origin_die)
17210 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17211 return origin_die;
17214 /* We do not currently support the pure_virtual attribute. */
17216 static inline void
17217 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17219 if (DECL_VINDEX (func_decl))
17221 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17223 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
17224 add_AT_loc (die, DW_AT_vtable_elem_location,
17225 new_loc_descr (DW_OP_constu,
17226 tree_to_shwi (DECL_VINDEX (func_decl)),
17227 0));
17229 /* GNU extension: Record what type this method came from originally. */
17230 if (debug_info_level > DINFO_LEVEL_TERSE
17231 && DECL_CONTEXT (func_decl))
17232 add_AT_die_ref (die, DW_AT_containing_type,
17233 lookup_type_die (DECL_CONTEXT (func_decl)));
17237 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17238 given decl. This used to be a vendor extension until after DWARF 4
17239 standardized it. */
17241 static void
17242 add_linkage_attr (dw_die_ref die, tree decl)
17244 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17246 /* Mimic what assemble_name_raw does with a leading '*'. */
17247 if (name[0] == '*')
17248 name = &name[1];
17250 if (dwarf_version >= 4)
17251 add_AT_string (die, DW_AT_linkage_name, name);
17252 else
17253 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17256 /* Add source coordinate attributes for the given decl. */
17258 static void
17259 add_src_coords_attributes (dw_die_ref die, tree decl)
17261 expanded_location s;
17263 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
17264 return;
17265 s = expand_location (DECL_SOURCE_LOCATION (decl));
17266 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17267 add_AT_unsigned (die, DW_AT_decl_line, s.line);
17270 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
17272 static void
17273 add_linkage_name (dw_die_ref die, tree decl)
17275 if (debug_info_level > DINFO_LEVEL_NONE
17276 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17277 && TREE_PUBLIC (decl)
17278 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17279 && die->die_tag != DW_TAG_member)
17281 /* Defer until we have an assembler name set. */
17282 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17284 limbo_die_node *asm_name;
17286 asm_name = ggc_cleared_alloc<limbo_die_node> ();
17287 asm_name->die = die;
17288 asm_name->created_for = decl;
17289 asm_name->next = deferred_asm_name;
17290 deferred_asm_name = asm_name;
17292 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17293 add_linkage_attr (die, decl);
17297 /* Add a DW_AT_name attribute and source coordinate attribute for the
17298 given decl, but only if it actually has a name. */
17300 static void
17301 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17303 tree decl_name;
17305 decl_name = DECL_NAME (decl);
17306 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17308 const char *name = dwarf2_name (decl, 0);
17309 if (name)
17310 add_name_attribute (die, name);
17311 if (! DECL_ARTIFICIAL (decl))
17312 add_src_coords_attributes (die, decl);
17314 add_linkage_name (die, decl);
17317 #ifdef VMS_DEBUGGING_INFO
17318 /* Get the function's name, as described by its RTL. This may be different
17319 from the DECL_NAME name used in the source file. */
17320 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17322 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17323 XEXP (DECL_RTL (decl), 0), false);
17324 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
17326 #endif /* VMS_DEBUGGING_INFO */
17329 #ifdef VMS_DEBUGGING_INFO
17330 /* Output the debug main pointer die for VMS */
17332 void
17333 dwarf2out_vms_debug_main_pointer (void)
17335 char label[MAX_ARTIFICIAL_LABEL_BYTES];
17336 dw_die_ref die;
17338 /* Allocate the VMS debug main subprogram die. */
17339 die = ggc_cleared_alloc<die_node> ();
17340 die->die_tag = DW_TAG_subprogram;
17341 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17342 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17343 current_function_funcdef_no);
17344 add_AT_lbl_id (die, DW_AT_entry_pc, label);
17346 /* Make it the first child of comp_unit_die (). */
17347 die->die_parent = comp_unit_die ();
17348 if (comp_unit_die ()->die_child)
17350 die->die_sib = comp_unit_die ()->die_child->die_sib;
17351 comp_unit_die ()->die_child->die_sib = die;
17353 else
17355 die->die_sib = die;
17356 comp_unit_die ()->die_child = die;
17359 #endif /* VMS_DEBUGGING_INFO */
17361 /* Push a new declaration scope. */
17363 static void
17364 push_decl_scope (tree scope)
17366 vec_safe_push (decl_scope_table, scope);
17369 /* Pop a declaration scope. */
17371 static inline void
17372 pop_decl_scope (void)
17374 decl_scope_table->pop ();
17377 /* walk_tree helper function for uses_local_type, below. */
17379 static tree
17380 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
17382 if (!TYPE_P (*tp))
17383 *walk_subtrees = 0;
17384 else
17386 tree name = TYPE_NAME (*tp);
17387 if (name && DECL_P (name) && decl_function_context (name))
17388 return *tp;
17390 return NULL_TREE;
17393 /* If TYPE involves a function-local type (including a local typedef to a
17394 non-local type), returns that type; otherwise returns NULL_TREE. */
17396 static tree
17397 uses_local_type (tree type)
17399 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
17400 return used;
17403 /* Return the DIE for the scope that immediately contains this type.
17404 Non-named types that do not involve a function-local type get global
17405 scope. Named types nested in namespaces or other types get their
17406 containing scope. All other types (i.e. function-local named types) get
17407 the current active scope. */
17409 static dw_die_ref
17410 scope_die_for (tree t, dw_die_ref context_die)
17412 dw_die_ref scope_die = NULL;
17413 tree containing_scope;
17415 /* Non-types always go in the current scope. */
17416 gcc_assert (TYPE_P (t));
17418 /* Use the scope of the typedef, rather than the scope of the type
17419 it refers to. */
17420 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
17421 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
17422 else
17423 containing_scope = TYPE_CONTEXT (t);
17425 /* Use the containing namespace if there is one. */
17426 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17428 if (context_die == lookup_decl_die (containing_scope))
17429 /* OK */;
17430 else if (debug_info_level > DINFO_LEVEL_TERSE)
17431 context_die = get_context_die (containing_scope);
17432 else
17433 containing_scope = NULL_TREE;
17436 /* Ignore function type "scopes" from the C frontend. They mean that
17437 a tagged type is local to a parmlist of a function declarator, but
17438 that isn't useful to DWARF. */
17439 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17440 containing_scope = NULL_TREE;
17442 if (SCOPE_FILE_SCOPE_P (containing_scope))
17444 /* If T uses a local type keep it local as well, to avoid references
17445 to function-local DIEs from outside the function. */
17446 if (current_function_decl && uses_local_type (t))
17447 scope_die = context_die;
17448 else
17449 scope_die = comp_unit_die ();
17451 else if (TYPE_P (containing_scope))
17453 /* For types, we can just look up the appropriate DIE. */
17454 if (debug_info_level > DINFO_LEVEL_TERSE)
17455 scope_die = get_context_die (containing_scope);
17456 else
17458 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17459 if (scope_die == NULL)
17460 scope_die = comp_unit_die ();
17463 else
17464 scope_die = context_die;
17466 return scope_die;
17469 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17471 static inline int
17472 local_scope_p (dw_die_ref context_die)
17474 for (; context_die; context_die = context_die->die_parent)
17475 if (context_die->die_tag == DW_TAG_inlined_subroutine
17476 || context_die->die_tag == DW_TAG_subprogram)
17477 return 1;
17479 return 0;
17482 /* Returns nonzero if CONTEXT_DIE is a class. */
17484 static inline int
17485 class_scope_p (dw_die_ref context_die)
17487 return (context_die
17488 && (context_die->die_tag == DW_TAG_structure_type
17489 || context_die->die_tag == DW_TAG_class_type
17490 || context_die->die_tag == DW_TAG_interface_type
17491 || context_die->die_tag == DW_TAG_union_type));
17494 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17495 whether or not to treat a DIE in this context as a declaration. */
17497 static inline int
17498 class_or_namespace_scope_p (dw_die_ref context_die)
17500 return (class_scope_p (context_die)
17501 || (context_die && context_die->die_tag == DW_TAG_namespace));
17504 /* Many forms of DIEs require a "type description" attribute. This
17505 routine locates the proper "type descriptor" die for the type given
17506 by 'type' plus any additional qualifiers given by 'cv_quals', and
17507 adds a DW_AT_type attribute below the given die. */
17509 static void
17510 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17511 dw_die_ref context_die)
17513 enum tree_code code = TREE_CODE (type);
17514 dw_die_ref type_die = NULL;
17516 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17517 or fixed-point type, use the inner type. This is because we have no
17518 support for unnamed types in base_type_die. This can happen if this is
17519 an Ada subrange type. Correct solution is emit a subrange type die. */
17520 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17521 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17522 type = TREE_TYPE (type), code = TREE_CODE (type);
17524 if (code == ERROR_MARK
17525 /* Handle a special case. For functions whose return type is void, we
17526 generate *no* type attribute. (Note that no object may have type
17527 `void', so this only applies to function return types). */
17528 || code == VOID_TYPE)
17529 return;
17531 type_die = modified_type_die (type,
17532 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17533 context_die);
17535 if (type_die != NULL)
17536 add_AT_die_ref (object_die, DW_AT_type, type_die);
17539 /* Given an object die, add the calling convention attribute for the
17540 function call type. */
17541 static void
17542 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17544 enum dwarf_calling_convention value = DW_CC_normal;
17546 value = ((enum dwarf_calling_convention)
17547 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17549 if (is_fortran ()
17550 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17552 /* DWARF 2 doesn't provide a way to identify a program's source-level
17553 entry point. DW_AT_calling_convention attributes are only meant
17554 to describe functions' calling conventions. However, lacking a
17555 better way to signal the Fortran main program, we used this for
17556 a long time, following existing custom. Now, DWARF 4 has
17557 DW_AT_main_subprogram, which we add below, but some tools still
17558 rely on the old way, which we thus keep. */
17559 value = DW_CC_program;
17561 if (dwarf_version >= 4 || !dwarf_strict)
17562 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17565 /* Only add the attribute if the backend requests it, and
17566 is not DW_CC_normal. */
17567 if (value && (value != DW_CC_normal))
17568 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17571 /* Given a tree pointer to a struct, class, union, or enum type node, return
17572 a pointer to the (string) tag name for the given type, or zero if the type
17573 was declared without a tag. */
17575 static const char *
17576 type_tag (const_tree type)
17578 const char *name = 0;
17580 if (TYPE_NAME (type) != 0)
17582 tree t = 0;
17584 /* Find the IDENTIFIER_NODE for the type name. */
17585 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17586 && !TYPE_NAMELESS (type))
17587 t = TYPE_NAME (type);
17589 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17590 a TYPE_DECL node, regardless of whether or not a `typedef' was
17591 involved. */
17592 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17593 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17595 /* We want to be extra verbose. Don't call dwarf_name if
17596 DECL_NAME isn't set. The default hook for decl_printable_name
17597 doesn't like that, and in this context it's correct to return
17598 0, instead of "<anonymous>" or the like. */
17599 if (DECL_NAME (TYPE_NAME (type))
17600 && !DECL_NAMELESS (TYPE_NAME (type)))
17601 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17604 /* Now get the name as a string, or invent one. */
17605 if (!name && t != 0)
17606 name = IDENTIFIER_POINTER (t);
17609 return (name == 0 || *name == '\0') ? 0 : name;
17612 /* Return the type associated with a data member, make a special check
17613 for bit field types. */
17615 static inline tree
17616 member_declared_type (const_tree member)
17618 return (DECL_BIT_FIELD_TYPE (member)
17619 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17622 /* Get the decl's label, as described by its RTL. This may be different
17623 from the DECL_NAME name used in the source file. */
17625 #if 0
17626 static const char *
17627 decl_start_label (tree decl)
17629 rtx x;
17630 const char *fnname;
17632 x = DECL_RTL (decl);
17633 gcc_assert (MEM_P (x));
17635 x = XEXP (x, 0);
17636 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17638 fnname = XSTR (x, 0);
17639 return fnname;
17641 #endif
17643 /* For variable-length arrays that have been previously generated, but
17644 may be incomplete due to missing subscript info, fill the subscript
17645 info. Return TRUE if this is one of those cases. */
17646 static bool
17647 fill_variable_array_bounds (tree type)
17649 if (TREE_ASM_WRITTEN (type)
17650 && TREE_CODE (type) == ARRAY_TYPE
17651 && variably_modified_type_p (type, NULL))
17653 dw_die_ref array_die = lookup_type_die (type);
17654 if (!array_die)
17655 return false;
17656 add_subscript_info (array_die, type, !is_ada ());
17657 return true;
17659 return false;
17662 /* These routines generate the internal representation of the DIE's for
17663 the compilation unit. Debugging information is collected by walking
17664 the declaration trees passed in from dwarf2out_decl(). */
17666 static void
17667 gen_array_type_die (tree type, dw_die_ref context_die)
17669 dw_die_ref array_die;
17671 /* GNU compilers represent multidimensional array types as sequences of one
17672 dimensional array types whose element types are themselves array types.
17673 We sometimes squish that down to a single array_type DIE with multiple
17674 subscripts in the Dwarf debugging info. The draft Dwarf specification
17675 say that we are allowed to do this kind of compression in C, because
17676 there is no difference between an array of arrays and a multidimensional
17677 array. We don't do this for Ada to remain as close as possible to the
17678 actual representation, which is especially important against the language
17679 flexibilty wrt arrays of variable size. */
17681 bool collapse_nested_arrays = !is_ada ();
17683 if (fill_variable_array_bounds (type))
17684 return;
17686 dw_die_ref scope_die = scope_die_for (type, context_die);
17687 tree element_type;
17689 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17690 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17691 if (TYPE_STRING_FLAG (type)
17692 && TREE_CODE (type) == ARRAY_TYPE
17693 && is_fortran ()
17694 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17696 HOST_WIDE_INT size;
17698 array_die = new_die (DW_TAG_string_type, scope_die, type);
17699 add_name_attribute (array_die, type_tag (type));
17700 equate_type_number_to_die (type, array_die);
17701 size = int_size_in_bytes (type);
17702 if (size >= 0)
17703 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17704 else if (TYPE_DOMAIN (type) != NULL_TREE
17705 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17706 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17708 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17709 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
17711 size = int_size_in_bytes (TREE_TYPE (szdecl));
17712 if (loc && size > 0)
17714 add_AT_location_description (array_die, DW_AT_string_length, loc);
17715 if (size != DWARF2_ADDR_SIZE)
17716 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17719 return;
17722 array_die = new_die (DW_TAG_array_type, scope_die, type);
17723 add_name_attribute (array_die, type_tag (type));
17724 equate_type_number_to_die (type, array_die);
17726 if (TREE_CODE (type) == VECTOR_TYPE)
17727 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17729 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17730 if (is_fortran ()
17731 && TREE_CODE (type) == ARRAY_TYPE
17732 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17733 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17734 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17736 #if 0
17737 /* We default the array ordering. SDB will probably do
17738 the right things even if DW_AT_ordering is not present. It's not even
17739 an issue until we start to get into multidimensional arrays anyway. If
17740 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17741 then we'll have to put the DW_AT_ordering attribute back in. (But if
17742 and when we find out that we need to put these in, we will only do so
17743 for multidimensional arrays. */
17744 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17745 #endif
17747 if (TREE_CODE (type) == VECTOR_TYPE)
17749 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17750 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17751 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
17752 add_bound_info (subrange_die, DW_AT_upper_bound,
17753 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
17755 else
17756 add_subscript_info (array_die, type, collapse_nested_arrays);
17758 /* Add representation of the type of the elements of this array type and
17759 emit the corresponding DIE if we haven't done it already. */
17760 element_type = TREE_TYPE (type);
17761 if (collapse_nested_arrays)
17762 while (TREE_CODE (element_type) == ARRAY_TYPE)
17764 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17765 break;
17766 element_type = TREE_TYPE (element_type);
17769 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17771 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17772 if (TYPE_ARTIFICIAL (type))
17773 add_AT_flag (array_die, DW_AT_artificial, 1);
17775 if (get_AT (array_die, DW_AT_name))
17776 add_pubtype (type, array_die);
17779 /* This routine generates DIE for array with hidden descriptor, details
17780 are filled into *info by a langhook. */
17782 static void
17783 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17784 dw_die_ref context_die)
17786 const dw_die_ref scope_die = scope_die_for (type, context_die);
17787 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
17788 const struct loc_descr_context context = { type, info->base_decl };
17789 int dim;
17791 add_name_attribute (array_die, type_tag (type));
17792 equate_type_number_to_die (type, array_die);
17794 if (info->ndimensions > 1)
17795 switch (info->ordering)
17797 case array_descr_ordering_row_major:
17798 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17799 break;
17800 case array_descr_ordering_column_major:
17801 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17802 break;
17803 default:
17804 break;
17807 if (dwarf_version >= 3 || !dwarf_strict)
17809 if (info->data_location)
17810 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
17811 dw_scalar_form_exprloc, &context);
17812 if (info->associated)
17813 add_scalar_info (array_die, DW_AT_associated, info->associated,
17814 dw_scalar_form_constant
17815 | dw_scalar_form_exprloc
17816 | dw_scalar_form_reference, &context);
17817 if (info->allocated)
17818 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
17819 dw_scalar_form_constant
17820 | dw_scalar_form_exprloc
17821 | dw_scalar_form_reference, &context);
17824 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17826 for (dim = 0; dim < info->ndimensions; dim++)
17828 dw_die_ref subrange_die
17829 = new_die (DW_TAG_subrange_type, array_die, NULL);
17831 if (info->dimen[dim].bounds_type)
17832 add_type_attribute (subrange_die,
17833 info->dimen[dim].bounds_type, 0,
17834 context_die);
17835 if (info->dimen[dim].lower_bound)
17836 add_bound_info (subrange_die, DW_AT_lower_bound,
17837 info->dimen[dim].lower_bound, &context);
17838 if (info->dimen[dim].upper_bound)
17839 add_bound_info (subrange_die, DW_AT_upper_bound,
17840 info->dimen[dim].upper_bound, &context);
17841 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
17842 add_scalar_info (subrange_die, DW_AT_byte_stride,
17843 info->dimen[dim].stride,
17844 dw_scalar_form_constant
17845 | dw_scalar_form_exprloc
17846 | dw_scalar_form_reference,
17847 &context);
17850 gen_type_die (info->element_type, context_die);
17851 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17852 context_die);
17854 if (get_AT (array_die, DW_AT_name))
17855 add_pubtype (type, array_die);
17858 #if 0
17859 static void
17860 gen_entry_point_die (tree decl, dw_die_ref context_die)
17862 tree origin = decl_ultimate_origin (decl);
17863 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17865 if (origin != NULL)
17866 add_abstract_origin_attribute (decl_die, origin);
17867 else
17869 add_name_and_src_coords_attributes (decl_die, decl);
17870 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17871 TYPE_UNQUALIFIED, context_die);
17874 if (DECL_ABSTRACT_P (decl))
17875 equate_decl_number_to_die (decl, decl_die);
17876 else
17877 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17879 #endif
17881 /* Walk through the list of incomplete types again, trying once more to
17882 emit full debugging info for them. */
17884 static void
17885 retry_incomplete_types (void)
17887 int i;
17889 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17890 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17891 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17894 /* Determine what tag to use for a record type. */
17896 static enum dwarf_tag
17897 record_type_tag (tree type)
17899 if (! lang_hooks.types.classify_record)
17900 return DW_TAG_structure_type;
17902 switch (lang_hooks.types.classify_record (type))
17904 case RECORD_IS_STRUCT:
17905 return DW_TAG_structure_type;
17907 case RECORD_IS_CLASS:
17908 return DW_TAG_class_type;
17910 case RECORD_IS_INTERFACE:
17911 if (dwarf_version >= 3 || !dwarf_strict)
17912 return DW_TAG_interface_type;
17913 return DW_TAG_structure_type;
17915 default:
17916 gcc_unreachable ();
17920 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17921 include all of the information about the enumeration values also. Each
17922 enumerated type name/value is listed as a child of the enumerated type
17923 DIE. */
17925 static dw_die_ref
17926 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17928 dw_die_ref type_die = lookup_type_die (type);
17930 if (type_die == NULL)
17932 type_die = new_die (DW_TAG_enumeration_type,
17933 scope_die_for (type, context_die), type);
17934 equate_type_number_to_die (type, type_die);
17935 add_name_attribute (type_die, type_tag (type));
17936 if (dwarf_version >= 4 || !dwarf_strict)
17938 if (ENUM_IS_SCOPED (type))
17939 add_AT_flag (type_die, DW_AT_enum_class, 1);
17940 if (ENUM_IS_OPAQUE (type))
17941 add_AT_flag (type_die, DW_AT_declaration, 1);
17944 else if (! TYPE_SIZE (type))
17945 return type_die;
17946 else
17947 remove_AT (type_die, DW_AT_declaration);
17949 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17950 given enum type is incomplete, do not generate the DW_AT_byte_size
17951 attribute or the DW_AT_element_list attribute. */
17952 if (TYPE_SIZE (type))
17954 tree link;
17956 TREE_ASM_WRITTEN (type) = 1;
17957 add_byte_size_attribute (type_die, type);
17958 if (dwarf_version >= 3 || !dwarf_strict)
17960 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17961 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17962 context_die);
17964 if (TYPE_STUB_DECL (type) != NULL_TREE)
17966 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17967 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17970 /* If the first reference to this type was as the return type of an
17971 inline function, then it may not have a parent. Fix this now. */
17972 if (type_die->die_parent == NULL)
17973 add_child_die (scope_die_for (type, context_die), type_die);
17975 for (link = TYPE_VALUES (type);
17976 link != NULL; link = TREE_CHAIN (link))
17978 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17979 tree value = TREE_VALUE (link);
17981 add_name_attribute (enum_die,
17982 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17984 if (TREE_CODE (value) == CONST_DECL)
17985 value = DECL_INITIAL (value);
17987 if (simple_type_size_in_bits (TREE_TYPE (value))
17988 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17990 /* For constant forms created by add_AT_unsigned DWARF
17991 consumers (GDB, elfutils, etc.) always zero extend
17992 the value. Only when the actual value is negative
17993 do we need to use add_AT_int to generate a constant
17994 form that can represent negative values. */
17995 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17996 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17997 add_AT_unsigned (enum_die, DW_AT_const_value,
17998 (unsigned HOST_WIDE_INT) val);
17999 else
18000 add_AT_int (enum_die, DW_AT_const_value, val);
18002 else
18003 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
18004 that here. TODO: This should be re-worked to use correct
18005 signed/unsigned double tags for all cases. */
18006 add_AT_wide (enum_die, DW_AT_const_value, value);
18009 add_gnat_descriptive_type_attribute (type_die, type, context_die);
18010 if (TYPE_ARTIFICIAL (type))
18011 add_AT_flag (type_die, DW_AT_artificial, 1);
18013 else
18014 add_AT_flag (type_die, DW_AT_declaration, 1);
18016 add_pubtype (type, type_die);
18018 return type_die;
18021 /* Generate a DIE to represent either a real live formal parameter decl or to
18022 represent just the type of some formal parameter position in some function
18023 type.
18025 Note that this routine is a bit unusual because its argument may be a
18026 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18027 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18028 node. If it's the former then this function is being called to output a
18029 DIE to represent a formal parameter object (or some inlining thereof). If
18030 it's the latter, then this function is only being called to output a
18031 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18032 argument type of some subprogram type.
18033 If EMIT_NAME_P is true, name and source coordinate attributes
18034 are emitted. */
18036 static dw_die_ref
18037 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18038 dw_die_ref context_die)
18040 tree node_or_origin = node ? node : origin;
18041 tree ultimate_origin;
18042 dw_die_ref parm_die = NULL;
18044 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
18046 parm_die = lookup_decl_die (node);
18048 /* If the contexts differ, we may not be talking about the same
18049 thing. */
18050 if (parm_die && parm_die->die_parent != context_die)
18052 if (!DECL_ABSTRACT_P (node))
18054 /* This can happen when creating an inlined instance, in
18055 which case we need to create a new DIE that will get
18056 annotated with DW_AT_abstract_origin. */
18057 parm_die = NULL;
18059 else
18061 /* FIXME: Reuse DIE even with a differing context.
18063 This can happen when calling
18064 dwarf2out_abstract_function to build debug info for
18065 the abstract instance of a function for which we have
18066 already generated a DIE in
18067 dwarf2out_early_global_decl.
18069 Once we remove dwarf2out_abstract_function, we should
18070 have a call to gcc_unreachable here. */
18074 if (parm_die && parm_die->die_parent == NULL)
18076 /* Check that parm_die already has the right attributes that
18077 we would have added below. If any attributes are
18078 missing, fall through to add them. */
18079 if (! DECL_ABSTRACT_P (node_or_origin)
18080 && !get_AT (parm_die, DW_AT_location)
18081 && !get_AT (parm_die, DW_AT_const_value))
18082 /* We are missing location info, and are about to add it. */
18084 else
18086 add_child_die (context_die, parm_die);
18087 return parm_die;
18092 /* If we have a previously generated DIE, use it, unless this is an
18093 concrete instance (origin != NULL), in which case we need a new
18094 DIE with a corresponding DW_AT_abstract_origin. */
18095 bool reusing_die;
18096 if (parm_die && origin == NULL)
18097 reusing_die = true;
18098 else
18100 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
18101 reusing_die = false;
18104 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18106 case tcc_declaration:
18107 ultimate_origin = decl_ultimate_origin (node_or_origin);
18108 if (node || ultimate_origin)
18109 origin = ultimate_origin;
18111 if (reusing_die)
18112 goto add_location;
18114 if (origin != NULL)
18115 add_abstract_origin_attribute (parm_die, origin);
18116 else if (emit_name_p)
18117 add_name_and_src_coords_attributes (parm_die, node);
18118 if (origin == NULL
18119 || (! DECL_ABSTRACT_P (node_or_origin)
18120 && variably_modified_type_p (TREE_TYPE (node_or_origin),
18121 decl_function_context
18122 (node_or_origin))))
18124 tree type = TREE_TYPE (node_or_origin);
18125 if (decl_by_reference_p (node_or_origin))
18126 add_type_attribute (parm_die, TREE_TYPE (type),
18127 TYPE_UNQUALIFIED, context_die);
18128 else
18129 add_type_attribute (parm_die, type,
18130 decl_quals (node_or_origin),
18131 context_die);
18133 if (origin == NULL && DECL_ARTIFICIAL (node))
18134 add_AT_flag (parm_die, DW_AT_artificial, 1);
18135 add_location:
18136 if (node && node != origin)
18137 equate_decl_number_to_die (node, parm_die);
18138 if (! DECL_ABSTRACT_P (node_or_origin))
18139 add_location_or_const_value_attribute (parm_die, node_or_origin,
18140 node == NULL);
18142 break;
18144 case tcc_type:
18145 /* We were called with some kind of a ..._TYPE node. */
18146 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
18147 context_die);
18148 break;
18150 default:
18151 gcc_unreachable ();
18154 return parm_die;
18157 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18158 children DW_TAG_formal_parameter DIEs representing the arguments of the
18159 parameter pack.
18161 PARM_PACK must be a function parameter pack.
18162 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18163 must point to the subsequent arguments of the function PACK_ARG belongs to.
18164 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18165 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18166 following the last one for which a DIE was generated. */
18168 static dw_die_ref
18169 gen_formal_parameter_pack_die (tree parm_pack,
18170 tree pack_arg,
18171 dw_die_ref subr_die,
18172 tree *next_arg)
18174 tree arg;
18175 dw_die_ref parm_pack_die;
18177 gcc_assert (parm_pack
18178 && lang_hooks.function_parameter_pack_p (parm_pack)
18179 && subr_die);
18181 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18182 add_src_coords_attributes (parm_pack_die, parm_pack);
18184 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18186 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18187 parm_pack))
18188 break;
18189 gen_formal_parameter_die (arg, NULL,
18190 false /* Don't emit name attribute. */,
18191 parm_pack_die);
18193 if (next_arg)
18194 *next_arg = arg;
18195 return parm_pack_die;
18198 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18199 at the end of an (ANSI prototyped) formal parameters list. */
18201 static void
18202 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18204 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18207 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18208 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18209 parameters as specified in some function type specification (except for
18210 those which appear as part of a function *definition*). */
18212 static void
18213 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18215 tree link;
18216 tree formal_type = NULL;
18217 tree first_parm_type;
18218 tree arg;
18220 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18222 arg = DECL_ARGUMENTS (function_or_method_type);
18223 function_or_method_type = TREE_TYPE (function_or_method_type);
18225 else
18226 arg = NULL_TREE;
18228 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18230 /* Make our first pass over the list of formal parameter types and output a
18231 DW_TAG_formal_parameter DIE for each one. */
18232 for (link = first_parm_type; link; )
18234 dw_die_ref parm_die;
18236 formal_type = TREE_VALUE (link);
18237 if (formal_type == void_type_node)
18238 break;
18240 /* Output a (nameless) DIE to represent the formal parameter itself. */
18241 if (!POINTER_BOUNDS_TYPE_P (formal_type))
18243 parm_die = gen_formal_parameter_die (formal_type, NULL,
18244 true /* Emit name attribute. */,
18245 context_die);
18246 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18247 && link == first_parm_type)
18249 add_AT_flag (parm_die, DW_AT_artificial, 1);
18250 if (dwarf_version >= 3 || !dwarf_strict)
18251 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18253 else if (arg && DECL_ARTIFICIAL (arg))
18254 add_AT_flag (parm_die, DW_AT_artificial, 1);
18257 link = TREE_CHAIN (link);
18258 if (arg)
18259 arg = DECL_CHAIN (arg);
18262 /* If this function type has an ellipsis, add a
18263 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
18264 if (formal_type != void_type_node)
18265 gen_unspecified_parameters_die (function_or_method_type, context_die);
18267 /* Make our second (and final) pass over the list of formal parameter types
18268 and output DIEs to represent those types (as necessary). */
18269 for (link = TYPE_ARG_TYPES (function_or_method_type);
18270 link && TREE_VALUE (link);
18271 link = TREE_CHAIN (link))
18272 gen_type_die (TREE_VALUE (link), context_die);
18275 /* We want to generate the DIE for TYPE so that we can generate the
18276 die for MEMBER, which has been defined; we will need to refer back
18277 to the member declaration nested within TYPE. If we're trying to
18278 generate minimal debug info for TYPE, processing TYPE won't do the
18279 trick; we need to attach the member declaration by hand. */
18281 static void
18282 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18284 gen_type_die (type, context_die);
18286 /* If we're trying to avoid duplicate debug info, we may not have
18287 emitted the member decl for this function. Emit it now. */
18288 if (TYPE_STUB_DECL (type)
18289 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18290 && ! lookup_decl_die (member))
18292 dw_die_ref type_die;
18293 gcc_assert (!decl_ultimate_origin (member));
18295 push_decl_scope (type);
18296 type_die = lookup_type_die_strip_naming_typedef (type);
18297 if (TREE_CODE (member) == FUNCTION_DECL)
18298 gen_subprogram_die (member, type_die);
18299 else if (TREE_CODE (member) == FIELD_DECL)
18301 /* Ignore the nameless fields that are used to skip bits but handle
18302 C++ anonymous unions and structs. */
18303 if (DECL_NAME (member) != NULL_TREE
18304 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18305 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18307 gen_type_die (member_declared_type (member), type_die);
18308 gen_field_die (member, type_die);
18311 else
18312 gen_variable_die (member, NULL_TREE, type_die);
18314 pop_decl_scope ();
18318 /* Forward declare these functions, because they are mutually recursive
18319 with their set_block_* pairing functions. */
18320 static void set_decl_origin_self (tree);
18321 static void set_decl_abstract_flags (tree, vec<tree> &);
18323 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
18324 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
18325 that it points to the node itself, thus indicating that the node is its
18326 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
18327 the given node is NULL, recursively descend the decl/block tree which
18328 it is the root of, and for each other ..._DECL or BLOCK node contained
18329 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
18330 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
18331 values to point to themselves. */
18333 static void
18334 set_block_origin_self (tree stmt)
18336 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
18338 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
18341 tree local_decl;
18343 for (local_decl = BLOCK_VARS (stmt);
18344 local_decl != NULL_TREE;
18345 local_decl = DECL_CHAIN (local_decl))
18346 /* Do not recurse on nested functions since the inlining status
18347 of parent and child can be different as per the DWARF spec. */
18348 if (TREE_CODE (local_decl) != FUNCTION_DECL
18349 && !DECL_EXTERNAL (local_decl))
18350 set_decl_origin_self (local_decl);
18354 tree subblock;
18356 for (subblock = BLOCK_SUBBLOCKS (stmt);
18357 subblock != NULL_TREE;
18358 subblock = BLOCK_CHAIN (subblock))
18359 set_block_origin_self (subblock); /* Recurse. */
18364 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
18365 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
18366 node to so that it points to the node itself, thus indicating that the
18367 node represents its own (abstract) origin. Additionally, if the
18368 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
18369 the decl/block tree of which the given node is the root of, and for
18370 each other ..._DECL or BLOCK node contained therein whose
18371 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
18372 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
18373 point to themselves. */
18375 static void
18376 set_decl_origin_self (tree decl)
18378 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
18380 DECL_ABSTRACT_ORIGIN (decl) = decl;
18381 if (TREE_CODE (decl) == FUNCTION_DECL)
18383 tree arg;
18385 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18386 DECL_ABSTRACT_ORIGIN (arg) = arg;
18387 if (DECL_INITIAL (decl) != NULL_TREE
18388 && DECL_INITIAL (decl) != error_mark_node)
18389 set_block_origin_self (DECL_INITIAL (decl));
18394 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
18395 and if it wasn't 1 before, push it to abstract_vec vector.
18396 For all local decls and all local sub-blocks (recursively) do it
18397 too. */
18399 static void
18400 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
18402 tree local_decl;
18403 tree subblock;
18404 unsigned int i;
18406 if (!BLOCK_ABSTRACT (stmt))
18408 abstract_vec.safe_push (stmt);
18409 BLOCK_ABSTRACT (stmt) = 1;
18412 for (local_decl = BLOCK_VARS (stmt);
18413 local_decl != NULL_TREE;
18414 local_decl = DECL_CHAIN (local_decl))
18415 if (! DECL_EXTERNAL (local_decl))
18416 set_decl_abstract_flags (local_decl, abstract_vec);
18418 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18420 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
18421 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
18422 || TREE_CODE (local_decl) == PARM_DECL)
18423 set_decl_abstract_flags (local_decl, abstract_vec);
18426 for (subblock = BLOCK_SUBBLOCKS (stmt);
18427 subblock != NULL_TREE;
18428 subblock = BLOCK_CHAIN (subblock))
18429 set_block_abstract_flags (subblock, abstract_vec);
18432 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
18433 to 1 and if it wasn't 1 before, push to abstract_vec vector.
18434 In the case where the decl is a FUNCTION_DECL also set the abstract
18435 flags for all of the parameters, local vars, local
18436 blocks and sub-blocks (recursively). */
18438 static void
18439 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
18441 if (!DECL_ABSTRACT_P (decl))
18443 abstract_vec.safe_push (decl);
18444 DECL_ABSTRACT_P (decl) = 1;
18447 if (TREE_CODE (decl) == FUNCTION_DECL)
18449 tree arg;
18451 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18452 if (!DECL_ABSTRACT_P (arg))
18454 abstract_vec.safe_push (arg);
18455 DECL_ABSTRACT_P (arg) = 1;
18457 if (DECL_INITIAL (decl) != NULL_TREE
18458 && DECL_INITIAL (decl) != error_mark_node)
18459 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
18463 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18464 may later generate inlined and/or out-of-line instances of.
18466 FIXME: In the early-dwarf world, this function, and most of the
18467 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
18468 the abstract instance. All we would need to do is annotate
18469 the early DIE with the appropriate DW_AT_inline in late
18470 dwarf (perhaps in gen_inlined_subroutine_die).
18472 However, we can't do this yet, because LTO streaming of DIEs
18473 has not been implemented yet. */
18475 static void
18476 dwarf2out_abstract_function (tree decl)
18478 dw_die_ref old_die;
18479 tree save_fn;
18480 tree context;
18481 hash_table<decl_loc_hasher> *old_decl_loc_table;
18482 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18483 int old_call_site_count, old_tail_call_site_count;
18484 struct call_arg_loc_node *old_call_arg_locations;
18486 /* Make sure we have the actual abstract inline, not a clone. */
18487 decl = DECL_ORIGIN (decl);
18489 old_die = lookup_decl_die (decl);
18490 if (old_die && get_AT (old_die, DW_AT_inline))
18491 /* We've already generated the abstract instance. */
18492 return;
18494 /* We can be called while recursively when seeing block defining inlined subroutine
18495 DIE. Be sure to not clobber the outer location table nor use it or we would
18496 get locations in abstract instantces. */
18497 old_decl_loc_table = decl_loc_table;
18498 decl_loc_table = NULL;
18499 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18500 cached_dw_loc_list_table = NULL;
18501 old_call_arg_locations = call_arg_locations;
18502 call_arg_locations = NULL;
18503 old_call_site_count = call_site_count;
18504 call_site_count = -1;
18505 old_tail_call_site_count = tail_call_site_count;
18506 tail_call_site_count = -1;
18508 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18509 we don't get confused by DECL_ABSTRACT_P. */
18510 if (debug_info_level > DINFO_LEVEL_TERSE)
18512 context = decl_class_context (decl);
18513 if (context)
18514 gen_type_die_for_member
18515 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18518 /* Pretend we've just finished compiling this function. */
18519 save_fn = current_function_decl;
18520 current_function_decl = decl;
18522 auto_vec<tree, 64> abstract_vec;
18523 set_decl_abstract_flags (decl, abstract_vec);
18524 dwarf2out_decl (decl);
18525 unsigned int i;
18526 tree t;
18527 FOR_EACH_VEC_ELT (abstract_vec, i, t)
18528 if (TREE_CODE (t) == BLOCK)
18529 BLOCK_ABSTRACT (t) = 0;
18530 else
18531 DECL_ABSTRACT_P (t) = 0;
18533 current_function_decl = save_fn;
18534 decl_loc_table = old_decl_loc_table;
18535 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18536 call_arg_locations = old_call_arg_locations;
18537 call_site_count = old_call_site_count;
18538 tail_call_site_count = old_tail_call_site_count;
18541 /* Helper function of premark_used_types() which gets called through
18542 htab_traverse.
18544 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18545 marked as unused by prune_unused_types. */
18547 bool
18548 premark_used_types_helper (tree const &type, void *)
18550 dw_die_ref die;
18552 die = lookup_type_die (type);
18553 if (die != NULL)
18554 die->die_perennial_p = 1;
18555 return true;
18558 /* Helper function of premark_types_used_by_global_vars which gets called
18559 through htab_traverse.
18561 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18562 marked as unused by prune_unused_types. The DIE of the type is marked
18563 only if the global variable using the type will actually be emitted. */
18566 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18567 void *)
18569 struct types_used_by_vars_entry *entry;
18570 dw_die_ref die;
18572 entry = (struct types_used_by_vars_entry *) *slot;
18573 gcc_assert (entry->type != NULL
18574 && entry->var_decl != NULL);
18575 die = lookup_type_die (entry->type);
18576 if (die)
18578 /* Ask cgraph if the global variable really is to be emitted.
18579 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18580 varpool_node *node = varpool_node::get (entry->var_decl);
18581 if (node && node->definition)
18583 die->die_perennial_p = 1;
18584 /* Keep the parent DIEs as well. */
18585 while ((die = die->die_parent) && die->die_perennial_p == 0)
18586 die->die_perennial_p = 1;
18589 return 1;
18592 /* Mark all members of used_types_hash as perennial. */
18594 static void
18595 premark_used_types (struct function *fun)
18597 if (fun && fun->used_types_hash)
18598 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18601 /* Mark all members of types_used_by_vars_entry as perennial. */
18603 static void
18604 premark_types_used_by_global_vars (void)
18606 if (types_used_by_vars_hash)
18607 types_used_by_vars_hash
18608 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18611 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18612 for CA_LOC call arg loc node. */
18614 static dw_die_ref
18615 gen_call_site_die (tree decl, dw_die_ref subr_die,
18616 struct call_arg_loc_node *ca_loc)
18618 dw_die_ref stmt_die = NULL, die;
18619 tree block = ca_loc->block;
18621 while (block
18622 && block != DECL_INITIAL (decl)
18623 && TREE_CODE (block) == BLOCK)
18625 stmt_die = BLOCK_DIE (block);
18626 if (stmt_die)
18627 break;
18628 block = BLOCK_SUPERCONTEXT (block);
18630 if (stmt_die == NULL)
18631 stmt_die = subr_die;
18632 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18633 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18634 if (ca_loc->tail_call_p)
18635 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18636 if (ca_loc->symbol_ref)
18638 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18639 if (tdie)
18640 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18641 else
18642 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18644 return die;
18647 /* Generate a DIE to represent a declared function (either file-scope or
18648 block-local). */
18650 static void
18651 gen_subprogram_die (tree decl, dw_die_ref context_die)
18653 tree origin = decl_ultimate_origin (decl);
18654 dw_die_ref subr_die;
18655 dw_die_ref old_die = lookup_decl_die (decl);
18657 /* This function gets called multiple times for different stages of
18658 the debug process. For example, for func() in this code:
18660 namespace S
18662 void func() { ... }
18665 ...we get called 4 times. Twice in early debug and twice in
18666 late debug:
18668 Early debug
18669 -----------
18671 1. Once while generating func() within the namespace. This is
18672 the declaration. The declaration bit below is set, as the
18673 context is the namespace.
18675 A new DIE will be generated with DW_AT_declaration set.
18677 2. Once for func() itself. This is the specification. The
18678 declaration bit below is clear as the context is the CU.
18680 We will use the cached DIE from (1) to create a new DIE with
18681 DW_AT_specification pointing to the declaration in (1).
18683 Late debug via rest_of_handle_final()
18684 -------------------------------------
18686 3. Once generating func() within the namespace. This is also the
18687 declaration, as in (1), but this time we will early exit below
18688 as we have a cached DIE and a declaration needs no additional
18689 annotations (no locations), as the source declaration line
18690 info is enough.
18692 4. Once for func() itself. As in (2), this is the specification,
18693 but this time we will re-use the cached DIE, and just annotate
18694 it with the location information that should now be available.
18696 For something without namespaces, but with abstract instances, we
18697 are also called a multiple times:
18699 class Base
18701 public:
18702 Base (); // constructor declaration (1)
18705 Base::Base () { } // constructor specification (2)
18707 Early debug
18708 -----------
18710 1. Once for the Base() constructor by virtue of it being a
18711 member of the Base class. This is done via
18712 rest_of_type_compilation.
18714 This is a declaration, so a new DIE will be created with
18715 DW_AT_declaration.
18717 2. Once for the Base() constructor definition, but this time
18718 while generating the abstract instance of the base
18719 constructor (__base_ctor) which is being generated via early
18720 debug of reachable functions.
18722 Even though we have a cached version of the declaration (1),
18723 we will create a DW_AT_specification of the declaration DIE
18724 in (1).
18726 3. Once for the __base_ctor itself, but this time, we generate
18727 an DW_AT_abstract_origin version of the DW_AT_specification in
18728 (2).
18730 Late debug via rest_of_handle_final
18731 -----------------------------------
18733 4. One final time for the __base_ctor (which will have a cached
18734 DIE with DW_AT_abstract_origin created in (3). This time,
18735 we will just annotate the location information now
18736 available.
18738 int declaration = (current_function_decl != decl
18739 || class_or_namespace_scope_p (context_die));
18741 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18743 /* Now that the C++ front end lazily declares artificial member fns, we
18744 might need to retrofit the declaration into its class. */
18745 if (!declaration && !origin && !old_die
18746 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18747 && !class_or_namespace_scope_p (context_die)
18748 && debug_info_level > DINFO_LEVEL_TERSE)
18749 old_die = force_decl_die (decl);
18751 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
18752 if (origin != NULL)
18754 gcc_assert (!declaration || local_scope_p (context_die));
18756 /* Fixup die_parent for the abstract instance of a nested
18757 inline function. */
18758 if (old_die && old_die->die_parent == NULL)
18759 add_child_die (context_die, old_die);
18761 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
18763 /* If we have a DW_AT_abstract_origin we have a working
18764 cached version. */
18765 subr_die = old_die;
18767 else
18769 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18770 add_abstract_origin_attribute (subr_die, origin);
18771 /* This is where the actual code for a cloned function is.
18772 Let's emit linkage name attribute for it. This helps
18773 debuggers to e.g, set breakpoints into
18774 constructors/destructors when the user asks "break
18775 K::K". */
18776 add_linkage_name (subr_die, decl);
18779 /* A cached copy, possibly from early dwarf generation. Reuse as
18780 much as possible. */
18781 else if (old_die)
18783 /* A declaration that has been previously dumped needs no
18784 additional information. */
18785 if (declaration)
18786 return;
18788 if (!get_AT_flag (old_die, DW_AT_declaration)
18789 /* We can have a normal definition following an inline one in the
18790 case of redefinition of GNU C extern inlines.
18791 It seems reasonable to use AT_specification in this case. */
18792 && !get_AT (old_die, DW_AT_inline))
18794 /* Detect and ignore this case, where we are trying to output
18795 something we have already output. */
18796 if (get_AT (old_die, DW_AT_low_pc)
18797 || get_AT (old_die, DW_AT_ranges))
18798 return;
18800 /* If we have no location information, this must be a
18801 partially generated DIE from early dwarf generation.
18802 Fall through and generate it. */
18805 /* If the definition comes from the same place as the declaration,
18806 maybe use the old DIE. We always want the DIE for this function
18807 that has the *_pc attributes to be under comp_unit_die so the
18808 debugger can find it. We also need to do this for abstract
18809 instances of inlines, since the spec requires the out-of-line copy
18810 to have the same parent. For local class methods, this doesn't
18811 apply; we just use the old DIE. */
18812 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18813 struct dwarf_file_data * file_index = lookup_filename (s.file);
18814 if ((is_cu_die (old_die->die_parent)
18815 /* This condition fixes the inconsistency/ICE with the
18816 following Fortran test (or some derivative thereof) while
18817 building libgfortran:
18819 module some_m
18820 contains
18821 logical function funky (FLAG)
18822 funky = .true.
18823 end function
18824 end module
18826 || (old_die->die_parent
18827 && old_die->die_parent->die_tag == DW_TAG_module)
18828 || context_die == NULL)
18829 && (DECL_ARTIFICIAL (decl)
18830 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18831 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18832 == (unsigned) s.line))))
18834 subr_die = old_die;
18836 /* Clear out the declaration attribute, but leave the
18837 parameters so they can be augmented with location
18838 information later. Unless this was a declaration, in
18839 which case, wipe out the nameless parameters and recreate
18840 them further down. */
18841 if (remove_AT (subr_die, DW_AT_declaration))
18844 remove_AT (subr_die, DW_AT_object_pointer);
18845 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18848 /* Make a specification pointing to the previously built
18849 declaration. */
18850 else
18852 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18853 add_AT_specification (subr_die, old_die);
18854 add_pubname (decl, subr_die);
18855 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18856 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18857 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18858 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18860 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18861 emit the real type on the definition die. */
18862 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18864 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18865 if (die == auto_die || die == decltype_auto_die)
18866 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18867 TYPE_UNQUALIFIED, context_die);
18871 /* Create a fresh DIE for anything else. */
18872 else
18874 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18876 if (TREE_PUBLIC (decl))
18877 add_AT_flag (subr_die, DW_AT_external, 1);
18879 add_name_and_src_coords_attributes (subr_die, decl);
18880 add_pubname (decl, subr_die);
18881 if (debug_info_level > DINFO_LEVEL_TERSE)
18883 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18884 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18885 TYPE_UNQUALIFIED, context_die);
18888 add_pure_or_virtual_attribute (subr_die, decl);
18889 if (DECL_ARTIFICIAL (decl))
18890 add_AT_flag (subr_die, DW_AT_artificial, 1);
18892 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
18893 add_AT_flag (subr_die, DW_AT_noreturn, 1);
18895 add_accessibility_attribute (subr_die, decl);
18898 /* Unless we have an existing non-declaration DIE, equate the new
18899 DIE. */
18900 if (!old_die || is_declaration_die (old_die))
18901 equate_decl_number_to_die (decl, subr_die);
18903 if (declaration)
18905 if (!old_die || !get_AT (old_die, DW_AT_inline))
18907 add_AT_flag (subr_die, DW_AT_declaration, 1);
18909 /* If this is an explicit function declaration then generate
18910 a DW_AT_explicit attribute. */
18911 if (lang_hooks.decls.function_decl_explicit_p (decl)
18912 && (dwarf_version >= 3 || !dwarf_strict))
18913 add_AT_flag (subr_die, DW_AT_explicit, 1);
18915 /* If this is a C++11 deleted special function member then generate
18916 a DW_AT_GNU_deleted attribute. */
18917 if (lang_hooks.decls.function_decl_deleted_p (decl)
18918 && (! dwarf_strict))
18919 add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18922 /* Tag abstract instances with DW_AT_inline. */
18923 else if (DECL_ABSTRACT_P (decl))
18925 if (DECL_DECLARED_INLINE_P (decl))
18927 if (cgraph_function_possibly_inlined_p (decl))
18928 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18929 else
18930 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18932 else
18934 if (cgraph_function_possibly_inlined_p (decl))
18935 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18936 else
18937 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18940 if (DECL_DECLARED_INLINE_P (decl)
18941 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18942 add_AT_flag (subr_die, DW_AT_artificial, 1);
18944 /* For non DECL_EXTERNALs, if range information is available, fill
18945 the DIE with it. */
18946 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
18948 HOST_WIDE_INT cfa_fb_offset;
18950 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18952 if (!flag_reorder_blocks_and_partition)
18954 dw_fde_ref fde = fun->fde;
18955 if (fde->dw_fde_begin)
18957 /* We have already generated the labels. */
18958 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18959 fde->dw_fde_end, false);
18961 else
18963 /* Create start/end labels and add the range. */
18964 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18965 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18966 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18967 current_function_funcdef_no);
18968 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18969 current_function_funcdef_no);
18970 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18971 false);
18974 #if VMS_DEBUGGING_INFO
18975 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18976 Section 2.3 Prologue and Epilogue Attributes:
18977 When a breakpoint is set on entry to a function, it is generally
18978 desirable for execution to be suspended, not on the very first
18979 instruction of the function, but rather at a point after the
18980 function's frame has been set up, after any language defined local
18981 declaration processing has been completed, and before execution of
18982 the first statement of the function begins. Debuggers generally
18983 cannot properly determine where this point is. Similarly for a
18984 breakpoint set on exit from a function. The prologue and epilogue
18985 attributes allow a compiler to communicate the location(s) to use. */
18988 if (fde->dw_fde_vms_end_prologue)
18989 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18990 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18992 if (fde->dw_fde_vms_begin_epilogue)
18993 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18994 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18996 #endif
18999 else
19001 /* Generate pubnames entries for the split function code ranges. */
19002 dw_fde_ref fde = fun->fde;
19004 if (fde->dw_fde_second_begin)
19006 if (dwarf_version >= 3 || !dwarf_strict)
19008 /* We should use ranges for non-contiguous code section
19009 addresses. Use the actual code range for the initial
19010 section, since the HOT/COLD labels might precede an
19011 alignment offset. */
19012 bool range_list_added = false;
19013 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
19014 fde->dw_fde_end, &range_list_added,
19015 false);
19016 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
19017 fde->dw_fde_second_end,
19018 &range_list_added, false);
19019 if (range_list_added)
19020 add_ranges (NULL);
19022 else
19024 /* There is no real support in DW2 for this .. so we make
19025 a work-around. First, emit the pub name for the segment
19026 containing the function label. Then make and emit a
19027 simplified subprogram DIE for the second segment with the
19028 name pre-fixed by __hot/cold_sect_of_. We use the same
19029 linkage name for the second die so that gdb will find both
19030 sections when given "b foo". */
19031 const char *name = NULL;
19032 tree decl_name = DECL_NAME (decl);
19033 dw_die_ref seg_die;
19035 /* Do the 'primary' section. */
19036 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
19037 fde->dw_fde_end, false);
19039 /* Build a minimal DIE for the secondary section. */
19040 seg_die = new_die (DW_TAG_subprogram,
19041 subr_die->die_parent, decl);
19043 if (TREE_PUBLIC (decl))
19044 add_AT_flag (seg_die, DW_AT_external, 1);
19046 if (decl_name != NULL
19047 && IDENTIFIER_POINTER (decl_name) != NULL)
19049 name = dwarf2_name (decl, 1);
19050 if (! DECL_ARTIFICIAL (decl))
19051 add_src_coords_attributes (seg_die, decl);
19053 add_linkage_name (seg_die, decl);
19055 gcc_assert (name != NULL);
19056 add_pure_or_virtual_attribute (seg_die, decl);
19057 if (DECL_ARTIFICIAL (decl))
19058 add_AT_flag (seg_die, DW_AT_artificial, 1);
19060 name = concat ("__second_sect_of_", name, NULL);
19061 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
19062 fde->dw_fde_second_end, false);
19063 add_name_attribute (seg_die, name);
19064 if (want_pubnames ())
19065 add_pubname_string (name, seg_die);
19068 else
19069 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
19070 false);
19073 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19075 /* We define the "frame base" as the function's CFA. This is more
19076 convenient for several reasons: (1) It's stable across the prologue
19077 and epilogue, which makes it better than just a frame pointer,
19078 (2) With dwarf3, there exists a one-byte encoding that allows us
19079 to reference the .debug_frame data by proxy, but failing that,
19080 (3) We can at least reuse the code inspection and interpretation
19081 code that determines the CFA position at various points in the
19082 function. */
19083 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
19085 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19086 add_AT_loc (subr_die, DW_AT_frame_base, op);
19088 else
19090 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19091 if (list->dw_loc_next)
19092 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19093 else
19094 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19097 /* Compute a displacement from the "steady-state frame pointer" to
19098 the CFA. The former is what all stack slots and argument slots
19099 will reference in the rtl; the latter is what we've told the
19100 debugger about. We'll need to adjust all frame_base references
19101 by this displacement. */
19102 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19104 if (fun->static_chain_decl)
19105 add_AT_location_description
19106 (subr_die, DW_AT_static_link,
19107 loc_list_from_tree (fun->static_chain_decl, 2, NULL));
19110 /* Generate child dies for template paramaters. */
19111 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
19112 gen_generic_params_dies (decl);
19114 /* Now output descriptions of the arguments for this function. This gets
19115 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19116 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19117 `...' at the end of the formal parameter list. In order to find out if
19118 there was a trailing ellipsis or not, we must instead look at the type
19119 associated with the FUNCTION_DECL. This will be a node of type
19120 FUNCTION_TYPE. If the chain of type nodes hanging off of this
19121 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19122 an ellipsis at the end. */
19124 /* In the case where we are describing a mere function declaration, all we
19125 need to do here (and all we *can* do here) is to describe the *types* of
19126 its formal parameters. */
19127 if (debug_info_level <= DINFO_LEVEL_TERSE)
19129 else if (declaration)
19130 gen_formal_types_die (decl, subr_die);
19131 else
19133 /* Generate DIEs to represent all known formal parameters. */
19134 tree parm = DECL_ARGUMENTS (decl);
19135 tree generic_decl = early_dwarf
19136 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
19137 tree generic_decl_parm = generic_decl
19138 ? DECL_ARGUMENTS (generic_decl)
19139 : NULL;
19141 /* Now we want to walk the list of parameters of the function and
19142 emit their relevant DIEs.
19144 We consider the case of DECL being an instance of a generic function
19145 as well as it being a normal function.
19147 If DECL is an instance of a generic function we walk the
19148 parameters of the generic function declaration _and_ the parameters of
19149 DECL itself. This is useful because we want to emit specific DIEs for
19150 function parameter packs and those are declared as part of the
19151 generic function declaration. In that particular case,
19152 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19153 That DIE has children DIEs representing the set of arguments
19154 of the pack. Note that the set of pack arguments can be empty.
19155 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19156 children DIE.
19158 Otherwise, we just consider the parameters of DECL. */
19159 while (generic_decl_parm || parm)
19161 if (generic_decl_parm
19162 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19163 gen_formal_parameter_pack_die (generic_decl_parm,
19164 parm, subr_die,
19165 &parm);
19166 else if (parm && !POINTER_BOUNDS_P (parm))
19168 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19170 if (parm == DECL_ARGUMENTS (decl)
19171 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19172 && parm_die
19173 && (dwarf_version >= 3 || !dwarf_strict))
19174 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19176 parm = DECL_CHAIN (parm);
19178 else if (parm)
19179 parm = DECL_CHAIN (parm);
19181 if (generic_decl_parm)
19182 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19185 /* Decide whether we need an unspecified_parameters DIE at the end.
19186 There are 2 more cases to do this for: 1) the ansi ... declaration -
19187 this is detectable when the end of the arg list is not a
19188 void_type_node 2) an unprototyped function declaration (not a
19189 definition). This just means that we have no info about the
19190 parameters at all. */
19191 if (prototype_p (TREE_TYPE (decl)))
19193 /* This is the prototyped case, check for.... */
19194 if (stdarg_p (TREE_TYPE (decl)))
19195 gen_unspecified_parameters_die (decl, subr_die);
19197 else if (DECL_INITIAL (decl) == NULL_TREE)
19198 gen_unspecified_parameters_die (decl, subr_die);
19201 if (subr_die != old_die)
19202 /* Add the calling convention attribute if requested. */
19203 add_calling_convention_attribute (subr_die, decl);
19205 /* Output Dwarf info for all of the stuff within the body of the function
19206 (if it has one - it may be just a declaration).
19208 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19209 a function. This BLOCK actually represents the outermost binding contour
19210 for the function, i.e. the contour in which the function's formal
19211 parameters and labels get declared. Curiously, it appears that the front
19212 end doesn't actually put the PARM_DECL nodes for the current function onto
19213 the BLOCK_VARS list for this outer scope, but are strung off of the
19214 DECL_ARGUMENTS list for the function instead.
19216 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19217 the LABEL_DECL nodes for the function however, and we output DWARF info
19218 for those in decls_for_scope. Just within the `outer_scope' there will be
19219 a BLOCK node representing the function's outermost pair of curly braces,
19220 and any blocks used for the base and member initializers of a C++
19221 constructor function. */
19222 tree outer_scope = DECL_INITIAL (decl);
19223 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
19225 int call_site_note_count = 0;
19226 int tail_call_site_note_count = 0;
19228 /* Emit a DW_TAG_variable DIE for a named return value. */
19229 if (DECL_NAME (DECL_RESULT (decl)))
19230 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19232 /* The first time through decls_for_scope we will generate the
19233 DIEs for the locals. The second time, we fill in the
19234 location info. */
19235 decls_for_scope (outer_scope, subr_die);
19237 if (call_arg_locations && !dwarf_strict)
19239 struct call_arg_loc_node *ca_loc;
19240 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
19242 dw_die_ref die = NULL;
19243 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
19244 rtx arg, next_arg;
19246 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
19247 arg; arg = next_arg)
19249 dw_loc_descr_ref reg, val;
19250 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
19251 dw_die_ref cdie, tdie = NULL;
19253 next_arg = XEXP (arg, 1);
19254 if (REG_P (XEXP (XEXP (arg, 0), 0))
19255 && next_arg
19256 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
19257 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
19258 && REGNO (XEXP (XEXP (arg, 0), 0))
19259 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
19260 next_arg = XEXP (next_arg, 1);
19261 if (mode == VOIDmode)
19263 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
19264 if (mode == VOIDmode)
19265 mode = GET_MODE (XEXP (arg, 0));
19267 if (mode == VOIDmode || mode == BLKmode)
19268 continue;
19269 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
19271 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19272 tloc = XEXP (XEXP (arg, 0), 1);
19273 continue;
19275 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
19276 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
19278 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19279 tlocc = XEXP (XEXP (arg, 0), 1);
19280 continue;
19282 reg = NULL;
19283 if (REG_P (XEXP (XEXP (arg, 0), 0)))
19284 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
19285 VAR_INIT_STATUS_INITIALIZED);
19286 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
19288 rtx mem = XEXP (XEXP (arg, 0), 0);
19289 reg = mem_loc_descriptor (XEXP (mem, 0),
19290 get_address_mode (mem),
19291 GET_MODE (mem),
19292 VAR_INIT_STATUS_INITIALIZED);
19294 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
19295 == DEBUG_PARAMETER_REF)
19297 tree tdecl
19298 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
19299 tdie = lookup_decl_die (tdecl);
19300 if (tdie == NULL)
19301 continue;
19303 else
19304 continue;
19305 if (reg == NULL
19306 && GET_CODE (XEXP (XEXP (arg, 0), 0))
19307 != DEBUG_PARAMETER_REF)
19308 continue;
19309 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
19310 VOIDmode,
19311 VAR_INIT_STATUS_INITIALIZED);
19312 if (val == NULL)
19313 continue;
19314 if (die == NULL)
19315 die = gen_call_site_die (decl, subr_die, ca_loc);
19316 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
19317 NULL_TREE);
19318 if (reg != NULL)
19319 add_AT_loc (cdie, DW_AT_location, reg);
19320 else if (tdie != NULL)
19321 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
19322 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
19323 if (next_arg != XEXP (arg, 1))
19325 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
19326 if (mode == VOIDmode)
19327 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
19328 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
19329 0), 1),
19330 mode, VOIDmode,
19331 VAR_INIT_STATUS_INITIALIZED);
19332 if (val != NULL)
19333 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
19336 if (die == NULL
19337 && (ca_loc->symbol_ref || tloc))
19338 die = gen_call_site_die (decl, subr_die, ca_loc);
19339 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
19341 dw_loc_descr_ref tval = NULL;
19343 if (tloc != NULL_RTX)
19344 tval = mem_loc_descriptor (tloc,
19345 GET_MODE (tloc) == VOIDmode
19346 ? Pmode : GET_MODE (tloc),
19347 VOIDmode,
19348 VAR_INIT_STATUS_INITIALIZED);
19349 if (tval)
19350 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
19351 else if (tlocc != NULL_RTX)
19353 tval = mem_loc_descriptor (tlocc,
19354 GET_MODE (tlocc) == VOIDmode
19355 ? Pmode : GET_MODE (tlocc),
19356 VOIDmode,
19357 VAR_INIT_STATUS_INITIALIZED);
19358 if (tval)
19359 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
19360 tval);
19363 if (die != NULL)
19365 call_site_note_count++;
19366 if (ca_loc->tail_call_p)
19367 tail_call_site_note_count++;
19371 call_arg_locations = NULL;
19372 call_arg_loc_last = NULL;
19373 if (tail_call_site_count >= 0
19374 && tail_call_site_count == tail_call_site_note_count
19375 && !dwarf_strict)
19377 if (call_site_count >= 0
19378 && call_site_count == call_site_note_count)
19379 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
19380 else
19381 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
19383 call_site_count = -1;
19384 tail_call_site_count = -1;
19388 /* Returns a hash value for X (which really is a die_struct). */
19390 hashval_t
19391 block_die_hasher::hash (die_struct *d)
19393 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19396 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19397 as decl_id and die_parent of die_struct Y. */
19399 bool
19400 block_die_hasher::equal (die_struct *x, die_struct *y)
19402 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
19405 /* Return TRUE if DECL, which may have been previously generated as
19406 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
19407 true if decl (or its origin) is either an extern declaration or a
19408 class/namespace scoped declaration.
19410 The declare_in_namespace support causes us to get two DIEs for one
19411 variable, both of which are declarations. We want to avoid
19412 considering one to be a specification, so we must test for
19413 DECLARATION and DW_AT_declaration. */
19414 static inline bool
19415 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
19417 return (old_die && TREE_STATIC (decl) && !declaration
19418 && get_AT_flag (old_die, DW_AT_declaration) == 1);
19421 /* Return true if DECL is a local static. */
19423 static inline bool
19424 local_function_static (tree decl)
19426 gcc_assert (TREE_CODE (decl) == VAR_DECL);
19427 return TREE_STATIC (decl)
19428 && DECL_CONTEXT (decl)
19429 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
19432 /* Generate a DIE to represent a declared data object.
19433 Either DECL or ORIGIN must be non-null. */
19435 static void
19436 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19438 HOST_WIDE_INT off = 0;
19439 tree com_decl;
19440 tree decl_or_origin = decl ? decl : origin;
19441 tree ultimate_origin;
19442 dw_die_ref var_die;
19443 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19444 dw_die_ref origin_die = NULL;
19445 bool declaration = (DECL_EXTERNAL (decl_or_origin)
19446 || class_or_namespace_scope_p (context_die));
19447 bool specialization_p = false;
19449 ultimate_origin = decl_ultimate_origin (decl_or_origin);
19450 if (decl || ultimate_origin)
19451 origin = ultimate_origin;
19452 com_decl = fortran_common (decl_or_origin, &off);
19454 /* Symbol in common gets emitted as a child of the common block, in the form
19455 of a data member. */
19456 if (com_decl)
19458 dw_die_ref com_die;
19459 dw_loc_list_ref loc;
19460 die_node com_die_arg;
19462 var_die = lookup_decl_die (decl_or_origin);
19463 if (var_die)
19465 if (get_AT (var_die, DW_AT_location) == NULL)
19467 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
19468 if (loc)
19470 if (off)
19472 /* Optimize the common case. */
19473 if (single_element_loc_list_p (loc)
19474 && loc->expr->dw_loc_opc == DW_OP_addr
19475 && loc->expr->dw_loc_next == NULL
19476 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19477 == SYMBOL_REF)
19479 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19480 loc->expr->dw_loc_oprnd1.v.val_addr
19481 = plus_constant (GET_MODE (x), x , off);
19483 else
19484 loc_list_plus_const (loc, off);
19486 add_AT_location_description (var_die, DW_AT_location, loc);
19487 remove_AT (var_die, DW_AT_declaration);
19490 return;
19493 if (common_block_die_table == NULL)
19494 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
19496 com_die_arg.decl_id = DECL_UID (com_decl);
19497 com_die_arg.die_parent = context_die;
19498 com_die = common_block_die_table->find (&com_die_arg);
19499 loc = loc_list_from_tree (com_decl, 2, NULL);
19500 if (com_die == NULL)
19502 const char *cnam
19503 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19504 die_node **slot;
19506 com_die = new_die (DW_TAG_common_block, context_die, decl);
19507 add_name_and_src_coords_attributes (com_die, com_decl);
19508 if (loc)
19510 add_AT_location_description (com_die, DW_AT_location, loc);
19511 /* Avoid sharing the same loc descriptor between
19512 DW_TAG_common_block and DW_TAG_variable. */
19513 loc = loc_list_from_tree (com_decl, 2, NULL);
19515 else if (DECL_EXTERNAL (decl))
19516 add_AT_flag (com_die, DW_AT_declaration, 1);
19517 if (want_pubnames ())
19518 add_pubname_string (cnam, com_die); /* ??? needed? */
19519 com_die->decl_id = DECL_UID (com_decl);
19520 slot = common_block_die_table->find_slot (com_die, INSERT);
19521 *slot = com_die;
19523 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19525 add_AT_location_description (com_die, DW_AT_location, loc);
19526 loc = loc_list_from_tree (com_decl, 2, NULL);
19527 remove_AT (com_die, DW_AT_declaration);
19529 var_die = new_die (DW_TAG_variable, com_die, decl);
19530 add_name_and_src_coords_attributes (var_die, decl);
19531 add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
19532 context_die);
19533 add_AT_flag (var_die, DW_AT_external, 1);
19534 if (loc)
19536 if (off)
19538 /* Optimize the common case. */
19539 if (single_element_loc_list_p (loc)
19540 && loc->expr->dw_loc_opc == DW_OP_addr
19541 && loc->expr->dw_loc_next == NULL
19542 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19544 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19545 loc->expr->dw_loc_oprnd1.v.val_addr
19546 = plus_constant (GET_MODE (x), x, off);
19548 else
19549 loc_list_plus_const (loc, off);
19551 add_AT_location_description (var_die, DW_AT_location, loc);
19553 else if (DECL_EXTERNAL (decl))
19554 add_AT_flag (var_die, DW_AT_declaration, 1);
19555 equate_decl_number_to_die (decl, var_die);
19556 return;
19559 if (old_die)
19561 if (declaration)
19563 /* A declaration that has been previously dumped, needs no
19564 further annotations, since it doesn't need location on
19565 the second pass. */
19566 return;
19568 else if (decl_will_get_specification_p (old_die, decl, declaration)
19569 && !get_AT (old_die, DW_AT_specification))
19571 /* Fall-thru so we can make a new variable die along with a
19572 DW_AT_specification. */
19574 else if (origin && old_die->die_parent != context_die)
19576 /* If we will be creating an inlined instance, we need a
19577 new DIE that will get annotated with
19578 DW_AT_abstract_origin. Clear things so we can get a
19579 new DIE. */
19580 gcc_assert (!DECL_ABSTRACT_P (decl));
19581 old_die = NULL;
19583 else
19585 /* If a DIE was dumped early, it still needs location info.
19586 Skip to where we fill the location bits. */
19587 var_die = old_die;
19588 goto gen_variable_die_location;
19592 /* For static data members, the declaration in the class is supposed
19593 to have DW_TAG_member tag; the specification should still be
19594 DW_TAG_variable referencing the DW_TAG_member DIE. */
19595 if (declaration && class_scope_p (context_die))
19596 var_die = new_die (DW_TAG_member, context_die, decl);
19597 else
19598 var_die = new_die (DW_TAG_variable, context_die, decl);
19600 if (origin != NULL)
19601 origin_die = add_abstract_origin_attribute (var_die, origin);
19603 /* Loop unrolling can create multiple blocks that refer to the same
19604 static variable, so we must test for the DW_AT_declaration flag.
19606 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19607 copy decls and set the DECL_ABSTRACT_P flag on them instead of
19608 sharing them.
19610 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
19611 else if (decl_will_get_specification_p (old_die, decl, declaration))
19613 /* This is a definition of a C++ class level static. */
19614 add_AT_specification (var_die, old_die);
19615 specialization_p = true;
19616 if (DECL_NAME (decl))
19618 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19619 struct dwarf_file_data * file_index = lookup_filename (s.file);
19621 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19622 add_AT_file (var_die, DW_AT_decl_file, file_index);
19624 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19625 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19627 if (old_die->die_tag == DW_TAG_member)
19628 add_linkage_name (var_die, decl);
19631 else
19632 add_name_and_src_coords_attributes (var_die, decl);
19634 if ((origin == NULL && !specialization_p)
19635 || (origin != NULL
19636 && !DECL_ABSTRACT_P (decl_or_origin)
19637 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19638 decl_function_context
19639 (decl_or_origin))))
19641 tree type = TREE_TYPE (decl_or_origin);
19643 if (decl_by_reference_p (decl_or_origin))
19644 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19645 context_die);
19646 else
19647 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19648 context_die);
19651 if (origin == NULL && !specialization_p)
19653 if (TREE_PUBLIC (decl))
19654 add_AT_flag (var_die, DW_AT_external, 1);
19656 if (DECL_ARTIFICIAL (decl))
19657 add_AT_flag (var_die, DW_AT_artificial, 1);
19659 add_accessibility_attribute (var_die, decl);
19662 if (declaration)
19663 add_AT_flag (var_die, DW_AT_declaration, 1);
19665 if (decl && (DECL_ABSTRACT_P (decl)
19666 || !old_die || is_declaration_die (old_die)))
19667 equate_decl_number_to_die (decl, var_die);
19669 gen_variable_die_location:
19670 if (! declaration
19671 && (! DECL_ABSTRACT_P (decl_or_origin)
19672 /* Local static vars are shared between all clones/inlines,
19673 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19674 already set. */
19675 || (TREE_CODE (decl_or_origin) == VAR_DECL
19676 && TREE_STATIC (decl_or_origin)
19677 && DECL_RTL_SET_P (decl_or_origin)))
19678 /* When abstract origin already has DW_AT_location attribute, no need
19679 to add it again. */
19680 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19682 if (early_dwarf)
19683 add_pubname (decl_or_origin, var_die);
19684 else
19685 add_location_or_const_value_attribute (var_die, decl_or_origin,
19686 decl == NULL);
19688 else
19689 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19692 /* Generate a DIE to represent a named constant. */
19694 static void
19695 gen_const_die (tree decl, dw_die_ref context_die)
19697 dw_die_ref const_die;
19698 tree type = TREE_TYPE (decl);
19700 const_die = lookup_decl_die (decl);
19701 if (const_die)
19702 return;
19704 const_die = new_die (DW_TAG_constant, context_die, decl);
19705 equate_decl_number_to_die (decl, const_die);
19706 add_name_and_src_coords_attributes (const_die, decl);
19707 add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19708 if (TREE_PUBLIC (decl))
19709 add_AT_flag (const_die, DW_AT_external, 1);
19710 if (DECL_ARTIFICIAL (decl))
19711 add_AT_flag (const_die, DW_AT_artificial, 1);
19712 tree_add_const_value_attribute_for_decl (const_die, decl);
19715 /* Generate a DIE to represent a label identifier. */
19717 static void
19718 gen_label_die (tree decl, dw_die_ref context_die)
19720 tree origin = decl_ultimate_origin (decl);
19721 dw_die_ref lbl_die = lookup_decl_die (decl);
19722 rtx insn;
19723 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19725 if (!lbl_die)
19727 lbl_die = new_die (DW_TAG_label, context_die, decl);
19728 equate_decl_number_to_die (decl, lbl_die);
19730 if (origin != NULL)
19731 add_abstract_origin_attribute (lbl_die, origin);
19732 else
19733 add_name_and_src_coords_attributes (lbl_die, decl);
19736 if (DECL_ABSTRACT_P (decl))
19737 equate_decl_number_to_die (decl, lbl_die);
19738 else
19740 insn = DECL_RTL_IF_SET (decl);
19742 /* Deleted labels are programmer specified labels which have been
19743 eliminated because of various optimizations. We still emit them
19744 here so that it is possible to put breakpoints on them. */
19745 if (insn
19746 && (LABEL_P (insn)
19747 || ((NOTE_P (insn)
19748 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19750 /* When optimization is enabled (via -O) some parts of the compiler
19751 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19752 represent source-level labels which were explicitly declared by
19753 the user. This really shouldn't be happening though, so catch
19754 it if it ever does happen. */
19755 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19757 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19758 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19760 else if (insn
19761 && NOTE_P (insn)
19762 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19763 && CODE_LABEL_NUMBER (insn) != -1)
19765 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19766 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19771 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19772 attributes to the DIE for a block STMT, to describe where the inlined
19773 function was called from. This is similar to add_src_coords_attributes. */
19775 static inline void
19776 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19778 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19780 if (dwarf_version >= 3 || !dwarf_strict)
19782 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19783 add_AT_unsigned (die, DW_AT_call_line, s.line);
19788 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19789 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19791 static inline void
19792 add_high_low_attributes (tree stmt, dw_die_ref die)
19794 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19796 if (BLOCK_FRAGMENT_CHAIN (stmt)
19797 && (dwarf_version >= 3 || !dwarf_strict))
19799 tree chain, superblock = NULL_TREE;
19800 dw_die_ref pdie;
19801 dw_attr_node *attr = NULL;
19803 if (inlined_function_outer_scope_p (stmt))
19805 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19806 BLOCK_NUMBER (stmt));
19807 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19810 /* Optimize duplicate .debug_ranges lists or even tails of
19811 lists. If this BLOCK has same ranges as its supercontext,
19812 lookup DW_AT_ranges attribute in the supercontext (and
19813 recursively so), verify that the ranges_table contains the
19814 right values and use it instead of adding a new .debug_range. */
19815 for (chain = stmt, pdie = die;
19816 BLOCK_SAME_RANGE (chain);
19817 chain = BLOCK_SUPERCONTEXT (chain))
19819 dw_attr_node *new_attr;
19821 pdie = pdie->die_parent;
19822 if (pdie == NULL)
19823 break;
19824 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19825 break;
19826 new_attr = get_AT (pdie, DW_AT_ranges);
19827 if (new_attr == NULL
19828 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19829 break;
19830 attr = new_attr;
19831 superblock = BLOCK_SUPERCONTEXT (chain);
19833 if (attr != NULL
19834 && (ranges_table[attr->dw_attr_val.v.val_offset
19835 / 2 / DWARF2_ADDR_SIZE].num
19836 == BLOCK_NUMBER (superblock))
19837 && BLOCK_FRAGMENT_CHAIN (superblock))
19839 unsigned long off = attr->dw_attr_val.v.val_offset
19840 / 2 / DWARF2_ADDR_SIZE;
19841 unsigned long supercnt = 0, thiscnt = 0;
19842 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19843 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19845 ++supercnt;
19846 gcc_checking_assert (ranges_table[off + supercnt].num
19847 == BLOCK_NUMBER (chain));
19849 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19850 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19851 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19852 ++thiscnt;
19853 gcc_assert (supercnt >= thiscnt);
19854 add_AT_range_list (die, DW_AT_ranges,
19855 ((off + supercnt - thiscnt)
19856 * 2 * DWARF2_ADDR_SIZE),
19857 false);
19858 return;
19861 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19863 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19866 add_ranges (chain);
19867 chain = BLOCK_FRAGMENT_CHAIN (chain);
19869 while (chain);
19870 add_ranges (NULL);
19872 else
19874 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19875 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19876 BLOCK_NUMBER (stmt));
19877 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19878 BLOCK_NUMBER (stmt));
19879 add_AT_low_high_pc (die, label, label_high, false);
19883 /* Generate a DIE for a lexical block. */
19885 static void
19886 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
19888 dw_die_ref old_die = BLOCK_DIE (stmt);
19889 dw_die_ref stmt_die = NULL;
19890 if (!old_die)
19892 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19893 BLOCK_DIE (stmt) = stmt_die;
19896 if (BLOCK_ABSTRACT (stmt))
19898 if (old_die)
19900 /* This must have been generated early and it won't even
19901 need location information since it's a DW_AT_inline
19902 function. */
19903 if (flag_checking)
19904 for (dw_die_ref c = context_die; c; c = c->die_parent)
19905 if (c->die_tag == DW_TAG_inlined_subroutine
19906 || c->die_tag == DW_TAG_subprogram)
19908 gcc_assert (get_AT (c, DW_AT_inline));
19909 break;
19911 return;
19914 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
19916 /* If this is an inlined instance, create a new lexical die for
19917 anything below to attach DW_AT_abstract_origin to. */
19918 if (old_die)
19920 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19921 BLOCK_DIE (stmt) = stmt_die;
19922 old_die = NULL;
19926 if (old_die)
19927 stmt_die = old_die;
19929 /* A non abstract block whose blocks have already been reordered
19930 should have the instruction range for this block. If so, set the
19931 high/low attributes. */
19932 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19934 gcc_assert (stmt_die);
19935 add_high_low_attributes (stmt, stmt_die);
19938 decls_for_scope (stmt, stmt_die);
19941 /* Generate a DIE for an inlined subprogram. */
19943 static void
19944 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
19946 tree decl;
19948 /* The instance of function that is effectively being inlined shall not
19949 be abstract. */
19950 gcc_assert (! BLOCK_ABSTRACT (stmt));
19952 decl = block_ultimate_origin (stmt);
19954 /* Make sure any inlined functions are known to be inlineable. */
19955 gcc_checking_assert (DECL_ABSTRACT_P (decl)
19956 || cgraph_function_possibly_inlined_p (decl));
19958 /* Emit info for the abstract instance first, if we haven't yet. We
19959 must emit this even if the block is abstract, otherwise when we
19960 emit the block below (or elsewhere), we may end up trying to emit
19961 a die whose origin die hasn't been emitted, and crashing. */
19962 dwarf2out_abstract_function (decl);
19964 if (! BLOCK_ABSTRACT (stmt))
19966 dw_die_ref subr_die
19967 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19969 if (call_arg_locations)
19970 BLOCK_DIE (stmt) = subr_die;
19971 add_abstract_origin_attribute (subr_die, decl);
19972 if (TREE_ASM_WRITTEN (stmt))
19973 add_high_low_attributes (stmt, subr_die);
19974 add_call_src_coords_attributes (stmt, subr_die);
19976 decls_for_scope (stmt, subr_die);
19980 /* Generate a DIE for a field in a record, or structure. */
19982 static void
19983 gen_field_die (tree decl, dw_die_ref context_die)
19985 dw_die_ref decl_die;
19987 if (TREE_TYPE (decl) == error_mark_node)
19988 return;
19990 decl_die = new_die (DW_TAG_member, context_die, decl);
19991 add_name_and_src_coords_attributes (decl_die, decl);
19992 add_type_attribute (decl_die, member_declared_type (decl),
19993 decl_quals (decl), context_die);
19995 if (DECL_BIT_FIELD_TYPE (decl))
19997 add_byte_size_attribute (decl_die, decl);
19998 add_bit_size_attribute (decl_die, decl);
19999 add_bit_offset_attribute (decl_die, decl);
20002 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
20003 add_data_member_location_attribute (decl_die, decl);
20005 if (DECL_ARTIFICIAL (decl))
20006 add_AT_flag (decl_die, DW_AT_artificial, 1);
20008 add_accessibility_attribute (decl_die, decl);
20010 /* Equate decl number to die, so that we can look up this decl later on. */
20011 equate_decl_number_to_die (decl, decl_die);
20014 #if 0
20015 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20016 Use modified_type_die instead.
20017 We keep this code here just in case these types of DIEs may be needed to
20018 represent certain things in other languages (e.g. Pascal) someday. */
20020 static void
20021 gen_pointer_type_die (tree type, dw_die_ref context_die)
20023 dw_die_ref ptr_die
20024 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
20026 equate_type_number_to_die (type, ptr_die);
20027 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20028 context_die);
20029 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20032 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20033 Use modified_type_die instead.
20034 We keep this code here just in case these types of DIEs may be needed to
20035 represent certain things in other languages (e.g. Pascal) someday. */
20037 static void
20038 gen_reference_type_die (tree type, dw_die_ref context_die)
20040 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
20042 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
20043 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
20044 else
20045 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
20047 equate_type_number_to_die (type, ref_die);
20048 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20049 context_die);
20050 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20052 #endif
20054 /* Generate a DIE for a pointer to a member type. */
20056 static void
20057 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
20059 dw_die_ref ptr_die
20060 = new_die (DW_TAG_ptr_to_member_type,
20061 scope_die_for (type, context_die), type);
20063 equate_type_number_to_die (type, ptr_die);
20064 add_AT_die_ref (ptr_die, DW_AT_containing_type,
20065 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
20066 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20067 context_die);
20070 static char *producer_string;
20072 /* Return a heap allocated producer string including command line options
20073 if -grecord-gcc-switches. */
20075 static char *
20076 gen_producer_string (void)
20078 size_t j;
20079 auto_vec<const char *> switches;
20080 const char *language_string = lang_hooks.name;
20081 char *producer, *tail;
20082 const char *p;
20083 size_t len = dwarf_record_gcc_switches ? 0 : 3;
20084 size_t plen = strlen (language_string) + 1 + strlen (version_string);
20086 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
20087 switch (save_decoded_options[j].opt_index)
20089 case OPT_o:
20090 case OPT_d:
20091 case OPT_dumpbase:
20092 case OPT_dumpdir:
20093 case OPT_auxbase:
20094 case OPT_auxbase_strip:
20095 case OPT_quiet:
20096 case OPT_version:
20097 case OPT_v:
20098 case OPT_w:
20099 case OPT_L:
20100 case OPT_D:
20101 case OPT_I:
20102 case OPT_U:
20103 case OPT_SPECIAL_unknown:
20104 case OPT_SPECIAL_ignore:
20105 case OPT_SPECIAL_program_name:
20106 case OPT_SPECIAL_input_file:
20107 case OPT_grecord_gcc_switches:
20108 case OPT_gno_record_gcc_switches:
20109 case OPT__output_pch_:
20110 case OPT_fdiagnostics_show_location_:
20111 case OPT_fdiagnostics_show_option:
20112 case OPT_fdiagnostics_show_caret:
20113 case OPT_fdiagnostics_color_:
20114 case OPT_fverbose_asm:
20115 case OPT____:
20116 case OPT__sysroot_:
20117 case OPT_nostdinc:
20118 case OPT_nostdinc__:
20119 case OPT_fpreprocessed:
20120 case OPT_fltrans_output_list_:
20121 case OPT_fresolution_:
20122 /* Ignore these. */
20123 continue;
20124 default:
20125 if (cl_options[save_decoded_options[j].opt_index].flags
20126 & CL_NO_DWARF_RECORD)
20127 continue;
20128 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
20129 == '-');
20130 switch (save_decoded_options[j].canonical_option[0][1])
20132 case 'M':
20133 case 'i':
20134 case 'W':
20135 continue;
20136 case 'f':
20137 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
20138 "dump", 4) == 0)
20139 continue;
20140 break;
20141 default:
20142 break;
20144 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
20145 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
20146 break;
20149 producer = XNEWVEC (char, plen + 1 + len + 1);
20150 tail = producer;
20151 sprintf (tail, "%s %s", language_string, version_string);
20152 tail += plen;
20154 FOR_EACH_VEC_ELT (switches, j, p)
20156 len = strlen (p);
20157 *tail = ' ';
20158 memcpy (tail + 1, p, len);
20159 tail += len + 1;
20162 *tail = '\0';
20163 return producer;
20166 /* Given a C and/or C++ language/version string return the "highest".
20167 C++ is assumed to be "higher" than C in this case. Used for merging
20168 LTO translation unit languages. */
20169 static const char *
20170 highest_c_language (const char *lang1, const char *lang2)
20172 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
20173 return "GNU C++14";
20174 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
20175 return "GNU C++11";
20176 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
20177 return "GNU C++98";
20179 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
20180 return "GNU C11";
20181 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
20182 return "GNU C99";
20183 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
20184 return "GNU C89";
20186 gcc_unreachable ();
20190 /* Generate the DIE for the compilation unit. */
20192 static dw_die_ref
20193 gen_compile_unit_die (const char *filename)
20195 dw_die_ref die;
20196 const char *language_string = lang_hooks.name;
20197 int language;
20199 die = new_die (DW_TAG_compile_unit, NULL, NULL);
20201 if (filename)
20203 add_name_attribute (die, filename);
20204 /* Don't add cwd for <built-in>. */
20205 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
20206 add_comp_dir_attribute (die);
20209 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
20211 /* If our producer is LTO try to figure out a common language to use
20212 from the global list of translation units. */
20213 if (strcmp (language_string, "GNU GIMPLE") == 0)
20215 unsigned i;
20216 tree t;
20217 const char *common_lang = NULL;
20219 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
20221 if (!TRANSLATION_UNIT_LANGUAGE (t))
20222 continue;
20223 if (!common_lang)
20224 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
20225 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
20227 else if (strncmp (common_lang, "GNU C", 5) == 0
20228 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
20229 /* Mixing C and C++ is ok, use C++ in that case. */
20230 common_lang = highest_c_language (common_lang,
20231 TRANSLATION_UNIT_LANGUAGE (t));
20232 else
20234 /* Fall back to C. */
20235 common_lang = NULL;
20236 break;
20240 if (common_lang)
20241 language_string = common_lang;
20244 language = DW_LANG_C;
20245 if (strncmp (language_string, "GNU C", 5) == 0
20246 && ISDIGIT (language_string[5]))
20248 language = DW_LANG_C89;
20249 if (dwarf_version >= 3 || !dwarf_strict)
20251 if (strcmp (language_string, "GNU C89") != 0)
20252 language = DW_LANG_C99;
20254 if (dwarf_version >= 5 /* || !dwarf_strict */)
20255 if (strcmp (language_string, "GNU C11") == 0)
20256 language = DW_LANG_C11;
20259 else if (strncmp (language_string, "GNU C++", 7) == 0)
20261 language = DW_LANG_C_plus_plus;
20262 if (dwarf_version >= 5 /* || !dwarf_strict */)
20264 if (strcmp (language_string, "GNU C++11") == 0)
20265 language = DW_LANG_C_plus_plus_11;
20266 else if (strcmp (language_string, "GNU C++14") == 0)
20267 language = DW_LANG_C_plus_plus_14;
20270 else if (strcmp (language_string, "GNU F77") == 0)
20271 language = DW_LANG_Fortran77;
20272 else if (strcmp (language_string, "GNU Pascal") == 0)
20273 language = DW_LANG_Pascal83;
20274 else if (dwarf_version >= 3 || !dwarf_strict)
20276 if (strcmp (language_string, "GNU Ada") == 0)
20277 language = DW_LANG_Ada95;
20278 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
20280 language = DW_LANG_Fortran95;
20281 if (dwarf_version >= 5 /* || !dwarf_strict */)
20283 if (strcmp (language_string, "GNU Fortran2003") == 0)
20284 language = DW_LANG_Fortran03;
20285 else if (strcmp (language_string, "GNU Fortran2008") == 0)
20286 language = DW_LANG_Fortran08;
20289 else if (strcmp (language_string, "GNU Java") == 0)
20290 language = DW_LANG_Java;
20291 else if (strcmp (language_string, "GNU Objective-C") == 0)
20292 language = DW_LANG_ObjC;
20293 else if (strcmp (language_string, "GNU Objective-C++") == 0)
20294 language = DW_LANG_ObjC_plus_plus;
20295 else if (dwarf_version >= 5 || !dwarf_strict)
20297 if (strcmp (language_string, "GNU Go") == 0)
20298 language = DW_LANG_Go;
20301 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
20302 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
20303 language = DW_LANG_Fortran90;
20305 add_AT_unsigned (die, DW_AT_language, language);
20307 switch (language)
20309 case DW_LANG_Fortran77:
20310 case DW_LANG_Fortran90:
20311 case DW_LANG_Fortran95:
20312 case DW_LANG_Fortran03:
20313 case DW_LANG_Fortran08:
20314 /* Fortran has case insensitive identifiers and the front-end
20315 lowercases everything. */
20316 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
20317 break;
20318 default:
20319 /* The default DW_ID_case_sensitive doesn't need to be specified. */
20320 break;
20322 return die;
20325 /* Generate the DIE for a base class. */
20327 static void
20328 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
20330 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
20332 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
20333 add_data_member_location_attribute (die, binfo);
20335 if (BINFO_VIRTUAL_P (binfo))
20336 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20338 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20339 children, otherwise the default is DW_ACCESS_public. In DWARF2
20340 the default has always been DW_ACCESS_private. */
20341 if (access == access_public_node)
20343 if (dwarf_version == 2
20344 || context_die->die_tag == DW_TAG_class_type)
20345 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20347 else if (access == access_protected_node)
20348 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20349 else if (dwarf_version > 2
20350 && context_die->die_tag != DW_TAG_class_type)
20351 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20354 /* Generate a DIE for a class member. */
20356 static void
20357 gen_member_die (tree type, dw_die_ref context_die)
20359 tree member;
20360 tree binfo = TYPE_BINFO (type);
20361 dw_die_ref child;
20363 /* If this is not an incomplete type, output descriptions of each of its
20364 members. Note that as we output the DIEs necessary to represent the
20365 members of this record or union type, we will also be trying to output
20366 DIEs to represent the *types* of those members. However the `type'
20367 function (above) will specifically avoid generating type DIEs for member
20368 types *within* the list of member DIEs for this (containing) type except
20369 for those types (of members) which are explicitly marked as also being
20370 members of this (containing) type themselves. The g++ front- end can
20371 force any given type to be treated as a member of some other (containing)
20372 type by setting the TYPE_CONTEXT of the given (member) type to point to
20373 the TREE node representing the appropriate (containing) type. */
20375 /* First output info about the base classes. */
20376 if (binfo)
20378 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
20379 int i;
20380 tree base;
20382 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
20383 gen_inheritance_die (base,
20384 (accesses ? (*accesses)[i] : access_public_node),
20385 context_die);
20388 /* Now output info about the data members and type members. */
20389 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
20391 /* If we thought we were generating minimal debug info for TYPE
20392 and then changed our minds, some of the member declarations
20393 may have already been defined. Don't define them again, but
20394 do put them in the right order. */
20396 child = lookup_decl_die (member);
20397 if (child)
20398 splice_child_die (context_die, child);
20399 else
20400 gen_decl_die (member, NULL, context_die);
20403 /* We do not keep type methods in type variants. */
20404 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
20405 /* Now output info about the function members (if any). */
20406 if (TYPE_METHODS (type) != error_mark_node)
20407 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
20409 /* Don't include clones in the member list. */
20410 if (DECL_ABSTRACT_ORIGIN (member))
20411 continue;
20412 /* Nor constructors for anonymous classes. */
20413 if (DECL_ARTIFICIAL (member)
20414 && dwarf2_name (member, 0) == NULL)
20415 continue;
20417 child = lookup_decl_die (member);
20418 if (child)
20419 splice_child_die (context_die, child);
20420 else
20421 gen_decl_die (member, NULL, context_die);
20425 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
20426 is set, we pretend that the type was never defined, so we only get the
20427 member DIEs needed by later specification DIEs. */
20429 static void
20430 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
20431 enum debug_info_usage usage)
20433 if (TREE_ASM_WRITTEN (type))
20435 /* Fill in the bound of variable-length fields in late dwarf if
20436 still incomplete. */
20437 if (!early_dwarf && variably_modified_type_p (type, NULL))
20438 for (tree member = TYPE_FIELDS (type);
20439 member;
20440 member = DECL_CHAIN (member))
20441 fill_variable_array_bounds (TREE_TYPE (member));
20442 return;
20445 dw_die_ref type_die = lookup_type_die (type);
20446 dw_die_ref scope_die = 0;
20447 int nested = 0;
20448 int complete = (TYPE_SIZE (type)
20449 && (! TYPE_STUB_DECL (type)
20450 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20451 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20452 complete = complete && should_emit_struct_debug (type, usage);
20454 if (type_die && ! complete)
20455 return;
20457 if (TYPE_CONTEXT (type) != NULL_TREE
20458 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20459 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20460 nested = 1;
20462 scope_die = scope_die_for (type, context_die);
20464 /* Generate child dies for template paramaters. */
20465 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
20466 schedule_generic_params_dies_gen (type);
20468 if (! type_die || (nested && is_cu_die (scope_die)))
20469 /* First occurrence of type or toplevel definition of nested class. */
20471 dw_die_ref old_die = type_die;
20473 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20474 ? record_type_tag (type) : DW_TAG_union_type,
20475 scope_die, type);
20476 equate_type_number_to_die (type, type_die);
20477 if (old_die)
20478 add_AT_specification (type_die, old_die);
20479 else
20480 add_name_attribute (type_die, type_tag (type));
20482 else
20483 remove_AT (type_die, DW_AT_declaration);
20485 /* If this type has been completed, then give it a byte_size attribute and
20486 then give a list of members. */
20487 if (complete && !ns_decl)
20489 /* Prevent infinite recursion in cases where the type of some member of
20490 this type is expressed in terms of this type itself. */
20491 TREE_ASM_WRITTEN (type) = 1;
20492 add_byte_size_attribute (type_die, type);
20493 if (TYPE_STUB_DECL (type) != NULL_TREE)
20495 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20496 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20499 /* If the first reference to this type was as the return type of an
20500 inline function, then it may not have a parent. Fix this now. */
20501 if (type_die->die_parent == NULL)
20502 add_child_die (scope_die, type_die);
20504 push_decl_scope (type);
20505 gen_member_die (type, type_die);
20506 pop_decl_scope ();
20508 add_gnat_descriptive_type_attribute (type_die, type, context_die);
20509 if (TYPE_ARTIFICIAL (type))
20510 add_AT_flag (type_die, DW_AT_artificial, 1);
20512 /* GNU extension: Record what type our vtable lives in. */
20513 if (TYPE_VFIELD (type))
20515 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20517 gen_type_die (vtype, context_die);
20518 add_AT_die_ref (type_die, DW_AT_containing_type,
20519 lookup_type_die (vtype));
20522 else
20524 add_AT_flag (type_die, DW_AT_declaration, 1);
20526 /* We don't need to do this for function-local types. */
20527 if (TYPE_STUB_DECL (type)
20528 && ! decl_function_context (TYPE_STUB_DECL (type)))
20529 vec_safe_push (incomplete_types, type);
20532 if (get_AT (type_die, DW_AT_name))
20533 add_pubtype (type, type_die);
20536 /* Generate a DIE for a subroutine _type_. */
20538 static void
20539 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20541 tree return_type = TREE_TYPE (type);
20542 dw_die_ref subr_die
20543 = new_die (DW_TAG_subroutine_type,
20544 scope_die_for (type, context_die), type);
20546 equate_type_number_to_die (type, subr_die);
20547 add_prototyped_attribute (subr_die, type);
20548 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
20549 gen_formal_types_die (type, subr_die);
20551 if (get_AT (subr_die, DW_AT_name))
20552 add_pubtype (type, subr_die);
20555 /* Generate a DIE for a type definition. */
20557 static void
20558 gen_typedef_die (tree decl, dw_die_ref context_die)
20560 dw_die_ref type_die;
20561 tree origin;
20563 if (TREE_ASM_WRITTEN (decl))
20565 if (DECL_ORIGINAL_TYPE (decl))
20566 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
20567 return;
20570 TREE_ASM_WRITTEN (decl) = 1;
20571 type_die = new_die (DW_TAG_typedef, context_die, decl);
20572 origin = decl_ultimate_origin (decl);
20573 if (origin != NULL)
20574 add_abstract_origin_attribute (type_die, origin);
20575 else
20577 tree type;
20579 add_name_and_src_coords_attributes (type_die, decl);
20580 if (DECL_ORIGINAL_TYPE (decl))
20582 type = DECL_ORIGINAL_TYPE (decl);
20584 if (type == error_mark_node)
20585 return;
20587 gcc_assert (type != TREE_TYPE (decl));
20588 equate_type_number_to_die (TREE_TYPE (decl), type_die);
20590 else
20592 type = TREE_TYPE (decl);
20594 if (type == error_mark_node)
20595 return;
20597 if (is_naming_typedef_decl (TYPE_NAME (type)))
20599 /* Here, we are in the case of decl being a typedef naming
20600 an anonymous type, e.g:
20601 typedef struct {...} foo;
20602 In that case TREE_TYPE (decl) is not a typedef variant
20603 type and TYPE_NAME of the anonymous type is set to the
20604 TYPE_DECL of the typedef. This construct is emitted by
20605 the C++ FE.
20607 TYPE is the anonymous struct named by the typedef
20608 DECL. As we need the DW_AT_type attribute of the
20609 DW_TAG_typedef to point to the DIE of TYPE, let's
20610 generate that DIE right away. add_type_attribute
20611 called below will then pick (via lookup_type_die) that
20612 anonymous struct DIE. */
20613 if (!TREE_ASM_WRITTEN (type))
20614 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20616 /* This is a GNU Extension. We are adding a
20617 DW_AT_linkage_name attribute to the DIE of the
20618 anonymous struct TYPE. The value of that attribute
20619 is the name of the typedef decl naming the anonymous
20620 struct. This greatly eases the work of consumers of
20621 this debug info. */
20622 add_linkage_attr (lookup_type_die (type), decl);
20626 add_type_attribute (type_die, type, decl_quals (decl), context_die);
20628 if (is_naming_typedef_decl (decl))
20629 /* We want that all subsequent calls to lookup_type_die with
20630 TYPE in argument yield the DW_TAG_typedef we have just
20631 created. */
20632 equate_type_number_to_die (type, type_die);
20634 add_accessibility_attribute (type_die, decl);
20637 if (DECL_ABSTRACT_P (decl))
20638 equate_decl_number_to_die (decl, type_die);
20640 if (get_AT (type_die, DW_AT_name))
20641 add_pubtype (decl, type_die);
20644 /* Generate a DIE for a struct, class, enum or union type. */
20646 static void
20647 gen_tagged_type_die (tree type,
20648 dw_die_ref context_die,
20649 enum debug_info_usage usage)
20651 int need_pop;
20653 if (type == NULL_TREE
20654 || !is_tagged_type (type))
20655 return;
20657 if (TREE_ASM_WRITTEN (type))
20658 need_pop = 0;
20659 /* If this is a nested type whose containing class hasn't been written
20660 out yet, writing it out will cover this one, too. This does not apply
20661 to instantiations of member class templates; they need to be added to
20662 the containing class as they are generated. FIXME: This hurts the
20663 idea of combining type decls from multiple TUs, since we can't predict
20664 what set of template instantiations we'll get. */
20665 else if (TYPE_CONTEXT (type)
20666 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20667 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20669 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20671 if (TREE_ASM_WRITTEN (type))
20672 return;
20674 /* If that failed, attach ourselves to the stub. */
20675 push_decl_scope (TYPE_CONTEXT (type));
20676 context_die = lookup_type_die (TYPE_CONTEXT (type));
20677 need_pop = 1;
20679 else if (TYPE_CONTEXT (type) != NULL_TREE
20680 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20682 /* If this type is local to a function that hasn't been written
20683 out yet, use a NULL context for now; it will be fixed up in
20684 decls_for_scope. */
20685 context_die = lookup_decl_die (TYPE_CONTEXT (type));
20686 /* A declaration DIE doesn't count; nested types need to go in the
20687 specification. */
20688 if (context_die && is_declaration_die (context_die))
20689 context_die = NULL;
20690 need_pop = 0;
20692 else
20694 context_die = declare_in_namespace (type, context_die);
20695 need_pop = 0;
20698 if (TREE_CODE (type) == ENUMERAL_TYPE)
20700 /* This might have been written out by the call to
20701 declare_in_namespace. */
20702 if (!TREE_ASM_WRITTEN (type))
20703 gen_enumeration_type_die (type, context_die);
20705 else
20706 gen_struct_or_union_type_die (type, context_die, usage);
20708 if (need_pop)
20709 pop_decl_scope ();
20711 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20712 it up if it is ever completed. gen_*_type_die will set it for us
20713 when appropriate. */
20716 /* Generate a type description DIE. */
20718 static void
20719 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20720 enum debug_info_usage usage)
20722 struct array_descr_info info;
20724 if (type == NULL_TREE || type == error_mark_node)
20725 return;
20727 if (flag_checking && type)
20728 verify_type (type);
20730 if (TYPE_NAME (type) != NULL_TREE
20731 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20732 && is_redundant_typedef (TYPE_NAME (type))
20733 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20734 /* The DECL of this type is a typedef we don't want to emit debug
20735 info for but we want debug info for its underlying typedef.
20736 This can happen for e.g, the injected-class-name of a C++
20737 type. */
20738 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20740 /* If TYPE is a typedef type variant, let's generate debug info
20741 for the parent typedef which TYPE is a type of. */
20742 if (typedef_variant_p (type))
20744 if (TREE_ASM_WRITTEN (type))
20745 return;
20747 /* Prevent broken recursion; we can't hand off to the same type. */
20748 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20750 /* Give typedefs the right scope. */
20751 context_die = scope_die_for (type, context_die);
20753 TREE_ASM_WRITTEN (type) = 1;
20755 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20756 return;
20759 /* If type is an anonymous tagged type named by a typedef, let's
20760 generate debug info for the typedef. */
20761 if (is_naming_typedef_decl (TYPE_NAME (type)))
20763 /* Use the DIE of the containing namespace as the parent DIE of
20764 the type description DIE we want to generate. */
20765 if (DECL_CONTEXT (TYPE_NAME (type))
20766 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20767 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20769 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20770 return;
20773 /* We are going to output a DIE to represent the unqualified version
20774 of this type (i.e. without any const or volatile qualifiers) so
20775 get the main variant (i.e. the unqualified version) of this type
20776 now. (Vectors are special because the debugging info is in the
20777 cloned type itself). */
20778 if (TREE_CODE (type) != VECTOR_TYPE)
20779 type = type_main_variant (type);
20781 /* If this is an array type with hidden descriptor, handle it first. */
20782 if (!TREE_ASM_WRITTEN (type)
20783 && lang_hooks.types.get_array_descr_info)
20785 memset (&info, 0, sizeof (info));
20786 if (lang_hooks.types.get_array_descr_info (type, &info))
20788 gen_descr_array_type_die (type, &info, context_die);
20789 TREE_ASM_WRITTEN (type) = 1;
20790 return;
20794 if (TREE_ASM_WRITTEN (type))
20796 /* Variable-length types may be incomplete even if
20797 TREE_ASM_WRITTEN. For such types, fall through to
20798 gen_array_type_die() and possibly fill in
20799 DW_AT_{upper,lower}_bound attributes. */
20800 if ((TREE_CODE (type) != ARRAY_TYPE
20801 && TREE_CODE (type) != RECORD_TYPE
20802 && TREE_CODE (type) != UNION_TYPE
20803 && TREE_CODE (type) != QUAL_UNION_TYPE)
20804 || !variably_modified_type_p (type, NULL))
20805 return;
20808 switch (TREE_CODE (type))
20810 case ERROR_MARK:
20811 break;
20813 case POINTER_TYPE:
20814 case REFERENCE_TYPE:
20815 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20816 ensures that the gen_type_die recursion will terminate even if the
20817 type is recursive. Recursive types are possible in Ada. */
20818 /* ??? We could perhaps do this for all types before the switch
20819 statement. */
20820 TREE_ASM_WRITTEN (type) = 1;
20822 /* For these types, all that is required is that we output a DIE (or a
20823 set of DIEs) to represent the "basis" type. */
20824 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20825 DINFO_USAGE_IND_USE);
20826 break;
20828 case OFFSET_TYPE:
20829 /* This code is used for C++ pointer-to-data-member types.
20830 Output a description of the relevant class type. */
20831 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20832 DINFO_USAGE_IND_USE);
20834 /* Output a description of the type of the object pointed to. */
20835 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20836 DINFO_USAGE_IND_USE);
20838 /* Now output a DIE to represent this pointer-to-data-member type
20839 itself. */
20840 gen_ptr_to_mbr_type_die (type, context_die);
20841 break;
20843 case FUNCTION_TYPE:
20844 /* Force out return type (in case it wasn't forced out already). */
20845 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20846 DINFO_USAGE_DIR_USE);
20847 gen_subroutine_type_die (type, context_die);
20848 break;
20850 case METHOD_TYPE:
20851 /* Force out return type (in case it wasn't forced out already). */
20852 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20853 DINFO_USAGE_DIR_USE);
20854 gen_subroutine_type_die (type, context_die);
20855 break;
20857 case ARRAY_TYPE:
20858 case VECTOR_TYPE:
20859 gen_array_type_die (type, context_die);
20860 break;
20862 case ENUMERAL_TYPE:
20863 case RECORD_TYPE:
20864 case UNION_TYPE:
20865 case QUAL_UNION_TYPE:
20866 gen_tagged_type_die (type, context_die, usage);
20867 return;
20869 case VOID_TYPE:
20870 case INTEGER_TYPE:
20871 case REAL_TYPE:
20872 case FIXED_POINT_TYPE:
20873 case COMPLEX_TYPE:
20874 case BOOLEAN_TYPE:
20875 case POINTER_BOUNDS_TYPE:
20876 /* No DIEs needed for fundamental types. */
20877 break;
20879 case NULLPTR_TYPE:
20880 case LANG_TYPE:
20881 /* Just use DW_TAG_unspecified_type. */
20883 dw_die_ref type_die = lookup_type_die (type);
20884 if (type_die == NULL)
20886 tree name = TYPE_IDENTIFIER (type);
20887 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20888 type);
20889 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20890 equate_type_number_to_die (type, type_die);
20893 break;
20895 default:
20896 if (is_cxx_auto (type))
20898 tree name = TYPE_IDENTIFIER (type);
20899 dw_die_ref *die = (name == get_identifier ("auto")
20900 ? &auto_die : &decltype_auto_die);
20901 if (!*die)
20903 *die = new_die (DW_TAG_unspecified_type,
20904 comp_unit_die (), NULL_TREE);
20905 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20907 equate_type_number_to_die (type, *die);
20908 break;
20910 gcc_unreachable ();
20913 TREE_ASM_WRITTEN (type) = 1;
20916 static void
20917 gen_type_die (tree type, dw_die_ref context_die)
20919 if (type != error_mark_node)
20921 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20922 if (flag_checking)
20924 dw_die_ref die = lookup_type_die (type);
20925 if (die)
20926 check_die (die);
20931 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20932 things which are local to the given block. */
20934 static void
20935 gen_block_die (tree stmt, dw_die_ref context_die)
20937 int must_output_die = 0;
20938 bool inlined_func;
20940 /* Ignore blocks that are NULL. */
20941 if (stmt == NULL_TREE)
20942 return;
20944 inlined_func = inlined_function_outer_scope_p (stmt);
20946 /* If the block is one fragment of a non-contiguous block, do not
20947 process the variables, since they will have been done by the
20948 origin block. Do process subblocks. */
20949 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20951 tree sub;
20953 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20954 gen_block_die (sub, context_die);
20956 return;
20959 /* Determine if we need to output any Dwarf DIEs at all to represent this
20960 block. */
20961 if (inlined_func)
20962 /* The outer scopes for inlinings *must* always be represented. We
20963 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20964 must_output_die = 1;
20965 else
20967 /* Determine if this block directly contains any "significant"
20968 local declarations which we will need to output DIEs for. */
20969 if (debug_info_level > DINFO_LEVEL_TERSE)
20970 /* We are not in terse mode so *any* local declaration counts
20971 as being a "significant" one. */
20972 must_output_die = ((BLOCK_VARS (stmt) != NULL
20973 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20974 && (TREE_USED (stmt)
20975 || TREE_ASM_WRITTEN (stmt)
20976 || BLOCK_ABSTRACT (stmt)));
20977 else if ((TREE_USED (stmt)
20978 || TREE_ASM_WRITTEN (stmt)
20979 || BLOCK_ABSTRACT (stmt))
20980 && !dwarf2out_ignore_block (stmt))
20981 must_output_die = 1;
20984 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20985 DIE for any block which contains no significant local declarations at
20986 all. Rather, in such cases we just call `decls_for_scope' so that any
20987 needed Dwarf info for any sub-blocks will get properly generated. Note
20988 that in terse mode, our definition of what constitutes a "significant"
20989 local declaration gets restricted to include only inlined function
20990 instances and local (nested) function definitions. */
20991 if (must_output_die)
20993 if (inlined_func)
20995 /* If STMT block is abstract, that means we have been called
20996 indirectly from dwarf2out_abstract_function.
20997 That function rightfully marks the descendent blocks (of
20998 the abstract function it is dealing with) as being abstract,
20999 precisely to prevent us from emitting any
21000 DW_TAG_inlined_subroutine DIE as a descendent
21001 of an abstract function instance. So in that case, we should
21002 not call gen_inlined_subroutine_die.
21004 Later though, when cgraph asks dwarf2out to emit info
21005 for the concrete instance of the function decl into which
21006 the concrete instance of STMT got inlined, the later will lead
21007 to the generation of a DW_TAG_inlined_subroutine DIE. */
21008 if (! BLOCK_ABSTRACT (stmt))
21009 gen_inlined_subroutine_die (stmt, context_die);
21011 else
21012 gen_lexical_block_die (stmt, context_die);
21014 else
21015 decls_for_scope (stmt, context_die);
21018 /* Process variable DECL (or variable with origin ORIGIN) within
21019 block STMT and add it to CONTEXT_DIE. */
21020 static void
21021 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
21023 dw_die_ref die;
21024 tree decl_or_origin = decl ? decl : origin;
21026 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
21027 die = lookup_decl_die (decl_or_origin);
21028 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
21029 && TYPE_DECL_IS_STUB (decl_or_origin))
21030 die = lookup_type_die (TREE_TYPE (decl_or_origin));
21031 else
21032 die = NULL;
21034 if (die != NULL && die->die_parent == NULL)
21035 add_child_die (context_die, die);
21036 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
21038 if (early_dwarf)
21039 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
21040 stmt, context_die);
21042 else
21043 gen_decl_die (decl, origin, context_die);
21046 /* Generate all of the decls declared within a given scope and (recursively)
21047 all of its sub-blocks. */
21049 static void
21050 decls_for_scope (tree stmt, dw_die_ref context_die)
21052 tree decl;
21053 unsigned int i;
21054 tree subblocks;
21056 /* Ignore NULL blocks. */
21057 if (stmt == NULL_TREE)
21058 return;
21060 /* Output the DIEs to represent all of the data objects and typedefs
21061 declared directly within this block but not within any nested
21062 sub-blocks. Also, nested function and tag DIEs have been
21063 generated with a parent of NULL; fix that up now. We don't
21064 have to do this if we're at -g1. */
21065 if (debug_info_level > DINFO_LEVEL_TERSE)
21067 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
21068 process_scope_var (stmt, decl, NULL_TREE, context_die);
21069 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21070 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
21071 context_die);
21074 /* Even if we're at -g1, we need to process the subblocks in order to get
21075 inlined call information. */
21077 /* Output the DIEs to represent all sub-blocks (and the items declared
21078 therein) of this block. */
21079 for (subblocks = BLOCK_SUBBLOCKS (stmt);
21080 subblocks != NULL;
21081 subblocks = BLOCK_CHAIN (subblocks))
21082 gen_block_die (subblocks, context_die);
21085 /* Is this a typedef we can avoid emitting? */
21087 static inline int
21088 is_redundant_typedef (const_tree decl)
21090 if (TYPE_DECL_IS_STUB (decl))
21091 return 1;
21093 if (DECL_ARTIFICIAL (decl)
21094 && DECL_CONTEXT (decl)
21095 && is_tagged_type (DECL_CONTEXT (decl))
21096 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
21097 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
21098 /* Also ignore the artificial member typedef for the class name. */
21099 return 1;
21101 return 0;
21104 /* Return TRUE if TYPE is a typedef that names a type for linkage
21105 purposes. This kind of typedefs is produced by the C++ FE for
21106 constructs like:
21108 typedef struct {...} foo;
21110 In that case, there is no typedef variant type produced for foo.
21111 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
21112 struct type. */
21114 static bool
21115 is_naming_typedef_decl (const_tree decl)
21117 if (decl == NULL_TREE
21118 || TREE_CODE (decl) != TYPE_DECL
21119 || !is_tagged_type (TREE_TYPE (decl))
21120 || DECL_IS_BUILTIN (decl)
21121 || is_redundant_typedef (decl)
21122 /* It looks like Ada produces TYPE_DECLs that are very similar
21123 to C++ naming typedefs but that have different
21124 semantics. Let's be specific to c++ for now. */
21125 || !is_cxx ())
21126 return FALSE;
21128 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
21129 && TYPE_NAME (TREE_TYPE (decl)) == decl
21130 && (TYPE_STUB_DECL (TREE_TYPE (decl))
21131 != TYPE_NAME (TREE_TYPE (decl))));
21134 /* Looks up the DIE for a context. */
21136 static inline dw_die_ref
21137 lookup_context_die (tree context)
21139 if (context)
21141 /* Find die that represents this context. */
21142 if (TYPE_P (context))
21144 context = TYPE_MAIN_VARIANT (context);
21145 dw_die_ref ctx = lookup_type_die (context);
21146 if (!ctx)
21147 return NULL;
21148 return strip_naming_typedef (context, ctx);
21150 else
21151 return lookup_decl_die (context);
21153 return comp_unit_die ();
21156 /* Returns the DIE for a context. */
21158 static inline dw_die_ref
21159 get_context_die (tree context)
21161 if (context)
21163 /* Find die that represents this context. */
21164 if (TYPE_P (context))
21166 context = TYPE_MAIN_VARIANT (context);
21167 return strip_naming_typedef (context, force_type_die (context));
21169 else
21170 return force_decl_die (context);
21172 return comp_unit_die ();
21175 /* Returns the DIE for decl. A DIE will always be returned. */
21177 static dw_die_ref
21178 force_decl_die (tree decl)
21180 dw_die_ref decl_die;
21181 unsigned saved_external_flag;
21182 tree save_fn = NULL_TREE;
21183 decl_die = lookup_decl_die (decl);
21184 if (!decl_die)
21186 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
21188 decl_die = lookup_decl_die (decl);
21189 if (decl_die)
21190 return decl_die;
21192 switch (TREE_CODE (decl))
21194 case FUNCTION_DECL:
21195 /* Clear current_function_decl, so that gen_subprogram_die thinks
21196 that this is a declaration. At this point, we just want to force
21197 declaration die. */
21198 save_fn = current_function_decl;
21199 current_function_decl = NULL_TREE;
21200 gen_subprogram_die (decl, context_die);
21201 current_function_decl = save_fn;
21202 break;
21204 case VAR_DECL:
21205 /* Set external flag to force declaration die. Restore it after
21206 gen_decl_die() call. */
21207 saved_external_flag = DECL_EXTERNAL (decl);
21208 DECL_EXTERNAL (decl) = 1;
21209 gen_decl_die (decl, NULL, context_die);
21210 DECL_EXTERNAL (decl) = saved_external_flag;
21211 break;
21213 case NAMESPACE_DECL:
21214 if (dwarf_version >= 3 || !dwarf_strict)
21215 dwarf2out_decl (decl);
21216 else
21217 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
21218 decl_die = comp_unit_die ();
21219 break;
21221 case TRANSLATION_UNIT_DECL:
21222 decl_die = comp_unit_die ();
21223 break;
21225 default:
21226 gcc_unreachable ();
21229 /* We should be able to find the DIE now. */
21230 if (!decl_die)
21231 decl_die = lookup_decl_die (decl);
21232 gcc_assert (decl_die);
21235 return decl_die;
21238 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
21239 always returned. */
21241 static dw_die_ref
21242 force_type_die (tree type)
21244 dw_die_ref type_die;
21246 type_die = lookup_type_die (type);
21247 if (!type_die)
21249 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
21251 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
21252 context_die);
21253 gcc_assert (type_die);
21255 return type_die;
21258 /* Force out any required namespaces to be able to output DECL,
21259 and return the new context_die for it, if it's changed. */
21261 static dw_die_ref
21262 setup_namespace_context (tree thing, dw_die_ref context_die)
21264 tree context = (DECL_P (thing)
21265 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
21266 if (context && TREE_CODE (context) == NAMESPACE_DECL)
21267 /* Force out the namespace. */
21268 context_die = force_decl_die (context);
21270 return context_die;
21273 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
21274 type) within its namespace, if appropriate.
21276 For compatibility with older debuggers, namespace DIEs only contain
21277 declarations; all definitions are emitted at CU scope, with
21278 DW_AT_specification pointing to the declaration (like with class
21279 members). */
21281 static dw_die_ref
21282 declare_in_namespace (tree thing, dw_die_ref context_die)
21284 dw_die_ref ns_context;
21286 if (debug_info_level <= DINFO_LEVEL_TERSE)
21287 return context_die;
21289 /* External declarations in the local scope only need to be emitted
21290 once, not once in the namespace and once in the scope.
21292 This avoids declaring the `extern' below in the
21293 namespace DIE as well as in the innermost scope:
21295 namespace S
21297 int i=5;
21298 int foo()
21300 int i=8;
21301 extern int i;
21302 return i;
21306 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
21307 return context_die;
21309 /* If this decl is from an inlined function, then don't try to emit it in its
21310 namespace, as we will get confused. It would have already been emitted
21311 when the abstract instance of the inline function was emitted anyways. */
21312 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
21313 return context_die;
21315 ns_context = setup_namespace_context (thing, context_die);
21317 if (ns_context != context_die)
21319 if (is_fortran ())
21320 return ns_context;
21321 if (DECL_P (thing))
21322 gen_decl_die (thing, NULL, ns_context);
21323 else
21324 gen_type_die (thing, ns_context);
21326 return context_die;
21329 /* Generate a DIE for a namespace or namespace alias. */
21331 static void
21332 gen_namespace_die (tree decl, dw_die_ref context_die)
21334 dw_die_ref namespace_die;
21336 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
21337 they are an alias of. */
21338 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
21340 /* Output a real namespace or module. */
21341 context_die = setup_namespace_context (decl, comp_unit_die ());
21342 namespace_die = new_die (is_fortran ()
21343 ? DW_TAG_module : DW_TAG_namespace,
21344 context_die, decl);
21345 /* For Fortran modules defined in different CU don't add src coords. */
21346 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
21348 const char *name = dwarf2_name (decl, 0);
21349 if (name)
21350 add_name_attribute (namespace_die, name);
21352 else
21353 add_name_and_src_coords_attributes (namespace_die, decl);
21354 if (DECL_EXTERNAL (decl))
21355 add_AT_flag (namespace_die, DW_AT_declaration, 1);
21356 equate_decl_number_to_die (decl, namespace_die);
21358 else
21360 /* Output a namespace alias. */
21362 /* Force out the namespace we are an alias of, if necessary. */
21363 dw_die_ref origin_die
21364 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
21366 if (DECL_FILE_SCOPE_P (decl)
21367 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
21368 context_die = setup_namespace_context (decl, comp_unit_die ());
21369 /* Now create the namespace alias DIE. */
21370 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
21371 add_name_and_src_coords_attributes (namespace_die, decl);
21372 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
21373 equate_decl_number_to_die (decl, namespace_die);
21375 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
21376 if (want_pubnames ())
21377 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
21380 /* Generate Dwarf debug information for a decl described by DECL.
21381 The return value is currently only meaningful for PARM_DECLs,
21382 for all other decls it returns NULL. */
21384 static dw_die_ref
21385 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
21387 tree decl_or_origin = decl ? decl : origin;
21388 tree class_origin = NULL, ultimate_origin;
21390 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
21391 return NULL;
21393 /* Ignore pointer bounds decls. */
21394 if (DECL_P (decl_or_origin)
21395 && TREE_TYPE (decl_or_origin)
21396 && POINTER_BOUNDS_P (decl_or_origin))
21397 return NULL;
21399 switch (TREE_CODE (decl_or_origin))
21401 case ERROR_MARK:
21402 break;
21404 case CONST_DECL:
21405 if (!is_fortran () && !is_ada ())
21407 /* The individual enumerators of an enum type get output when we output
21408 the Dwarf representation of the relevant enum type itself. */
21409 break;
21412 /* Emit its type. */
21413 gen_type_die (TREE_TYPE (decl), context_die);
21415 /* And its containing namespace. */
21416 context_die = declare_in_namespace (decl, context_die);
21418 gen_const_die (decl, context_die);
21419 break;
21421 case FUNCTION_DECL:
21422 /* Don't output any DIEs to represent mere function declarations,
21423 unless they are class members or explicit block externs. */
21424 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
21425 && DECL_FILE_SCOPE_P (decl_or_origin)
21426 && (current_function_decl == NULL_TREE
21427 || DECL_ARTIFICIAL (decl_or_origin)))
21428 break;
21430 #if 0
21431 /* FIXME */
21432 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
21433 on local redeclarations of global functions. That seems broken. */
21434 if (current_function_decl != decl)
21435 /* This is only a declaration. */;
21436 #endif
21438 /* If we're emitting a clone, emit info for the abstract instance. */
21439 if (origin || DECL_ORIGIN (decl) != decl)
21440 dwarf2out_abstract_function (origin
21441 ? DECL_ORIGIN (origin)
21442 : DECL_ABSTRACT_ORIGIN (decl));
21444 /* If we're emitting an out-of-line copy of an inline function,
21445 emit info for the abstract instance and set up to refer to it. */
21446 else if (cgraph_function_possibly_inlined_p (decl)
21447 && ! DECL_ABSTRACT_P (decl)
21448 && ! class_or_namespace_scope_p (context_die)
21449 /* dwarf2out_abstract_function won't emit a die if this is just
21450 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
21451 that case, because that works only if we have a die. */
21452 && DECL_INITIAL (decl) != NULL_TREE)
21454 dwarf2out_abstract_function (decl);
21455 set_decl_origin_self (decl);
21458 /* Otherwise we're emitting the primary DIE for this decl. */
21459 else if (debug_info_level > DINFO_LEVEL_TERSE)
21461 /* Before we describe the FUNCTION_DECL itself, make sure that we
21462 have its containing type. */
21463 if (!origin)
21464 origin = decl_class_context (decl);
21465 if (origin != NULL_TREE)
21466 gen_type_die (origin, context_die);
21468 /* And its return type. */
21469 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
21471 /* And its virtual context. */
21472 if (DECL_VINDEX (decl) != NULL_TREE)
21473 gen_type_die (DECL_CONTEXT (decl), context_die);
21475 /* Make sure we have a member DIE for decl. */
21476 if (origin != NULL_TREE)
21477 gen_type_die_for_member (origin, decl, context_die);
21479 /* And its containing namespace. */
21480 context_die = declare_in_namespace (decl, context_die);
21483 /* Now output a DIE to represent the function itself. */
21484 if (decl)
21485 gen_subprogram_die (decl, context_die);
21486 break;
21488 case TYPE_DECL:
21489 /* If we are in terse mode, don't generate any DIEs to represent any
21490 actual typedefs. */
21491 if (debug_info_level <= DINFO_LEVEL_TERSE)
21492 break;
21494 /* In the special case of a TYPE_DECL node representing the declaration
21495 of some type tag, if the given TYPE_DECL is marked as having been
21496 instantiated from some other (original) TYPE_DECL node (e.g. one which
21497 was generated within the original definition of an inline function) we
21498 used to generate a special (abbreviated) DW_TAG_structure_type,
21499 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
21500 should be actually referencing those DIEs, as variable DIEs with that
21501 type would be emitted already in the abstract origin, so it was always
21502 removed during unused type prunning. Don't add anything in this
21503 case. */
21504 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21505 break;
21507 if (is_redundant_typedef (decl))
21508 gen_type_die (TREE_TYPE (decl), context_die);
21509 else
21510 /* Output a DIE to represent the typedef itself. */
21511 gen_typedef_die (decl, context_die);
21512 break;
21514 case LABEL_DECL:
21515 if (debug_info_level >= DINFO_LEVEL_NORMAL)
21516 gen_label_die (decl, context_die);
21517 break;
21519 case VAR_DECL:
21520 case RESULT_DECL:
21521 /* If we are in terse mode, don't generate any DIEs to represent any
21522 variable declarations or definitions. */
21523 if (debug_info_level <= DINFO_LEVEL_TERSE)
21524 break;
21526 /* Output any DIEs that are needed to specify the type of this data
21527 object. */
21528 if (decl_by_reference_p (decl_or_origin))
21529 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21530 else
21531 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21533 /* And its containing type. */
21534 class_origin = decl_class_context (decl_or_origin);
21535 if (class_origin != NULL_TREE)
21536 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21538 /* And its containing namespace. */
21539 context_die = declare_in_namespace (decl_or_origin, context_die);
21541 /* Now output the DIE to represent the data object itself. This gets
21542 complicated because of the possibility that the VAR_DECL really
21543 represents an inlined instance of a formal parameter for an inline
21544 function. */
21545 ultimate_origin = decl_ultimate_origin (decl_or_origin);
21546 if (ultimate_origin != NULL_TREE
21547 && TREE_CODE (ultimate_origin) == PARM_DECL)
21548 gen_formal_parameter_die (decl, origin,
21549 true /* Emit name attribute. */,
21550 context_die);
21551 else
21552 gen_variable_die (decl, origin, context_die);
21553 break;
21555 case FIELD_DECL:
21556 /* Ignore the nameless fields that are used to skip bits but handle C++
21557 anonymous unions and structs. */
21558 if (DECL_NAME (decl) != NULL_TREE
21559 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21560 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21562 gen_type_die (member_declared_type (decl), context_die);
21563 gen_field_die (decl, context_die);
21565 break;
21567 case PARM_DECL:
21568 if (DECL_BY_REFERENCE (decl_or_origin))
21569 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21570 else
21571 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21572 return gen_formal_parameter_die (decl, origin,
21573 true /* Emit name attribute. */,
21574 context_die);
21576 case NAMESPACE_DECL:
21577 case IMPORTED_DECL:
21578 if (dwarf_version >= 3 || !dwarf_strict)
21579 gen_namespace_die (decl, context_die);
21580 break;
21582 case NAMELIST_DECL:
21583 gen_namelist_decl (DECL_NAME (decl), context_die,
21584 NAMELIST_DECL_ASSOCIATED_DECL (decl));
21585 break;
21587 default:
21588 /* Probably some frontend-internal decl. Assume we don't care. */
21589 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21590 break;
21593 return NULL;
21596 /* Output initial debug information for global DECL. Called at the
21597 end of the parsing process.
21599 This is the initial debug generation process. As such, the DIEs
21600 generated may be incomplete. A later debug generation pass
21601 (dwarf2out_late_global_decl) will augment the information generated
21602 in this pass (e.g., with complete location info). */
21604 static void
21605 dwarf2out_early_global_decl (tree decl)
21607 set_early_dwarf s;
21609 /* gen_decl_die() will set DECL_ABSTRACT because
21610 cgraph_function_possibly_inlined_p() returns true. This is in
21611 turn will cause DW_AT_inline attributes to be set.
21613 This happens because at early dwarf generation, there is no
21614 cgraph information, causing cgraph_function_possibly_inlined_p()
21615 to return true. Trick cgraph_function_possibly_inlined_p()
21616 while we generate dwarf early. */
21617 bool save = symtab->global_info_ready;
21618 symtab->global_info_ready = true;
21620 /* We don't handle TYPE_DECLs. If required, they'll be reached via
21621 other DECLs and they can point to template types or other things
21622 that dwarf2out can't handle when done via dwarf2out_decl. */
21623 if (TREE_CODE (decl) != TYPE_DECL
21624 && TREE_CODE (decl) != PARM_DECL)
21626 tree save_fndecl = current_function_decl;
21627 if (TREE_CODE (decl) == FUNCTION_DECL)
21629 /* No cfun means the symbol has no body, so there's nothing
21630 to emit. */
21631 if (!DECL_STRUCT_FUNCTION (decl))
21632 goto early_decl_exit;
21634 current_function_decl = decl;
21636 dwarf2out_decl (decl);
21637 if (TREE_CODE (decl) == FUNCTION_DECL)
21638 current_function_decl = save_fndecl;
21640 early_decl_exit:
21641 symtab->global_info_ready = save;
21644 /* Output debug information for global decl DECL. Called from
21645 toplev.c after compilation proper has finished. */
21647 static void
21648 dwarf2out_late_global_decl (tree decl)
21650 /* We have to generate early debug late for LTO. */
21651 if (in_lto_p)
21652 dwarf2out_early_global_decl (decl);
21654 /* Fill-in any location information we were unable to determine
21655 on the first pass. */
21656 if (TREE_CODE (decl) == VAR_DECL
21657 && !POINTER_BOUNDS_P (decl))
21659 dw_die_ref die = lookup_decl_die (decl);
21660 if (die)
21661 add_location_or_const_value_attribute (die, decl, false);
21665 /* Output debug information for type decl DECL. Called from toplev.c
21666 and from language front ends (to record built-in types). */
21667 static void
21668 dwarf2out_type_decl (tree decl, int local)
21670 if (!local)
21672 set_early_dwarf s;
21673 dwarf2out_decl (decl);
21677 /* Output debug information for imported module or decl DECL.
21678 NAME is non-NULL name in the lexical block if the decl has been renamed.
21679 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21680 that DECL belongs to.
21681 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
21682 static void
21683 dwarf2out_imported_module_or_decl_1 (tree decl,
21684 tree name,
21685 tree lexical_block,
21686 dw_die_ref lexical_block_die)
21688 expanded_location xloc;
21689 dw_die_ref imported_die = NULL;
21690 dw_die_ref at_import_die;
21692 if (TREE_CODE (decl) == IMPORTED_DECL)
21694 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21695 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21696 gcc_assert (decl);
21698 else
21699 xloc = expand_location (input_location);
21701 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21703 at_import_die = force_type_die (TREE_TYPE (decl));
21704 /* For namespace N { typedef void T; } using N::T; base_type_die
21705 returns NULL, but DW_TAG_imported_declaration requires
21706 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
21707 if (!at_import_die)
21709 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21710 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21711 at_import_die = lookup_type_die (TREE_TYPE (decl));
21712 gcc_assert (at_import_die);
21715 else
21717 at_import_die = lookup_decl_die (decl);
21718 if (!at_import_die)
21720 /* If we're trying to avoid duplicate debug info, we may not have
21721 emitted the member decl for this field. Emit it now. */
21722 if (TREE_CODE (decl) == FIELD_DECL)
21724 tree type = DECL_CONTEXT (decl);
21726 if (TYPE_CONTEXT (type)
21727 && TYPE_P (TYPE_CONTEXT (type))
21728 && !should_emit_struct_debug (TYPE_CONTEXT (type),
21729 DINFO_USAGE_DIR_USE))
21730 return;
21731 gen_type_die_for_member (type, decl,
21732 get_context_die (TYPE_CONTEXT (type)));
21734 if (TREE_CODE (decl) == NAMELIST_DECL)
21735 at_import_die = gen_namelist_decl (DECL_NAME (decl),
21736 get_context_die (DECL_CONTEXT (decl)),
21737 NULL_TREE);
21738 else
21739 at_import_die = force_decl_die (decl);
21743 if (TREE_CODE (decl) == NAMESPACE_DECL)
21745 if (dwarf_version >= 3 || !dwarf_strict)
21746 imported_die = new_die (DW_TAG_imported_module,
21747 lexical_block_die,
21748 lexical_block);
21749 else
21750 return;
21752 else
21753 imported_die = new_die (DW_TAG_imported_declaration,
21754 lexical_block_die,
21755 lexical_block);
21757 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21758 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21759 if (name)
21760 add_AT_string (imported_die, DW_AT_name,
21761 IDENTIFIER_POINTER (name));
21762 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21765 /* Output debug information for imported module or decl DECL.
21766 NAME is non-NULL name in context if the decl has been renamed.
21767 CHILD is true if decl is one of the renamed decls as part of
21768 importing whole module. */
21770 static void
21771 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21772 bool child)
21774 /* dw_die_ref at_import_die; */
21775 dw_die_ref scope_die;
21777 if (debug_info_level <= DINFO_LEVEL_TERSE)
21778 return;
21780 gcc_assert (decl);
21782 set_early_dwarf s;
21784 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21785 We need decl DIE for reference and scope die. First, get DIE for the decl
21786 itself. */
21788 /* Get the scope die for decl context. Use comp_unit_die for global module
21789 or decl. If die is not found for non globals, force new die. */
21790 if (context
21791 && TYPE_P (context)
21792 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21793 return;
21795 if (!(dwarf_version >= 3 || !dwarf_strict))
21796 return;
21798 scope_die = get_context_die (context);
21800 if (child)
21802 gcc_assert (scope_die->die_child);
21803 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21804 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21805 scope_die = scope_die->die_child;
21808 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
21809 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21812 /* Output debug information for namelists. */
21814 static dw_die_ref
21815 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
21817 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
21818 tree value;
21819 unsigned i;
21821 if (debug_info_level <= DINFO_LEVEL_TERSE)
21822 return NULL;
21824 gcc_assert (scope_die != NULL);
21825 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
21826 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
21828 /* If there are no item_decls, we have a nondefining namelist, e.g.
21829 with USE association; hence, set DW_AT_declaration. */
21830 if (item_decls == NULL_TREE)
21832 add_AT_flag (nml_die, DW_AT_declaration, 1);
21833 return nml_die;
21836 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
21838 nml_item_ref_die = lookup_decl_die (value);
21839 if (!nml_item_ref_die)
21840 nml_item_ref_die = force_decl_die (value);
21842 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
21843 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
21845 return nml_die;
21849 /* Write the debugging output for DECL and return the DIE. */
21851 static void
21852 dwarf2out_decl (tree decl)
21854 dw_die_ref context_die = comp_unit_die ();
21856 switch (TREE_CODE (decl))
21858 case ERROR_MARK:
21859 return;
21861 case FUNCTION_DECL:
21862 /* What we would really like to do here is to filter out all mere
21863 file-scope declarations of file-scope functions which are never
21864 referenced later within this translation unit (and keep all of ones
21865 that *are* referenced later on) but we aren't clairvoyant, so we have
21866 no idea which functions will be referenced in the future (i.e. later
21867 on within the current translation unit). So here we just ignore all
21868 file-scope function declarations which are not also definitions. If
21869 and when the debugger needs to know something about these functions,
21870 it will have to hunt around and find the DWARF information associated
21871 with the definition of the function.
21873 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21874 nodes represent definitions and which ones represent mere
21875 declarations. We have to check DECL_INITIAL instead. That's because
21876 the C front-end supports some weird semantics for "extern inline"
21877 function definitions. These can get inlined within the current
21878 translation unit (and thus, we need to generate Dwarf info for their
21879 abstract instances so that the Dwarf info for the concrete inlined
21880 instances can have something to refer to) but the compiler never
21881 generates any out-of-lines instances of such things (despite the fact
21882 that they *are* definitions).
21884 The important point is that the C front-end marks these "extern
21885 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21886 them anyway. Note that the C++ front-end also plays some similar games
21887 for inline function definitions appearing within include files which
21888 also contain `#pragma interface' pragmas.
21890 If we are called from dwarf2out_abstract_function output a DIE
21891 anyway. We can end up here this way with early inlining and LTO
21892 where the inlined function is output in a different LTRANS unit
21893 or not at all. */
21894 if (DECL_INITIAL (decl) == NULL_TREE
21895 && ! DECL_ABSTRACT_P (decl))
21896 return;
21898 /* If we're a nested function, initially use a parent of NULL; if we're
21899 a plain function, this will be fixed up in decls_for_scope. If
21900 we're a method, it will be ignored, since we already have a DIE. */
21901 if (decl_function_context (decl)
21902 /* But if we're in terse mode, we don't care about scope. */
21903 && debug_info_level > DINFO_LEVEL_TERSE)
21904 context_die = NULL;
21905 break;
21907 case VAR_DECL:
21908 /* For local statics lookup proper context die. */
21909 if (local_function_static (decl))
21910 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21912 /* If we are in terse mode, don't generate any DIEs to represent any
21913 variable declarations or definitions. */
21914 if (debug_info_level <= DINFO_LEVEL_TERSE)
21915 return;
21916 break;
21918 case CONST_DECL:
21919 if (debug_info_level <= DINFO_LEVEL_TERSE)
21920 return;
21921 if (!is_fortran () && !is_ada ())
21922 return;
21923 if (TREE_STATIC (decl) && decl_function_context (decl))
21924 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21925 break;
21927 case NAMESPACE_DECL:
21928 case IMPORTED_DECL:
21929 if (debug_info_level <= DINFO_LEVEL_TERSE)
21930 return;
21931 if (lookup_decl_die (decl) != NULL)
21932 return;
21933 break;
21935 case TYPE_DECL:
21936 /* Don't emit stubs for types unless they are needed by other DIEs. */
21937 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21938 return;
21940 /* Don't bother trying to generate any DIEs to represent any of the
21941 normal built-in types for the language we are compiling. */
21942 if (DECL_IS_BUILTIN (decl))
21943 return;
21945 /* If we are in terse mode, don't generate any DIEs for types. */
21946 if (debug_info_level <= DINFO_LEVEL_TERSE)
21947 return;
21949 /* If we're a function-scope tag, initially use a parent of NULL;
21950 this will be fixed up in decls_for_scope. */
21951 if (decl_function_context (decl))
21952 context_die = NULL;
21954 break;
21956 case NAMELIST_DECL:
21957 break;
21959 default:
21960 return;
21963 gen_decl_die (decl, NULL, context_die);
21965 if (flag_checking)
21967 dw_die_ref die = lookup_decl_die (decl);
21968 if (die)
21969 check_die (die);
21973 /* Write the debugging output for DECL. */
21975 static void
21976 dwarf2out_function_decl (tree decl)
21978 dwarf2out_decl (decl);
21979 call_arg_locations = NULL;
21980 call_arg_loc_last = NULL;
21981 call_site_count = -1;
21982 tail_call_site_count = -1;
21983 decl_loc_table->empty ();
21984 cached_dw_loc_list_table->empty ();
21987 /* Output a marker (i.e. a label) for the beginning of the generated code for
21988 a lexical block. */
21990 static void
21991 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21992 unsigned int blocknum)
21994 switch_to_section (current_function_section ());
21995 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21998 /* Output a marker (i.e. a label) for the end of the generated code for a
21999 lexical block. */
22001 static void
22002 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
22004 switch_to_section (current_function_section ());
22005 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
22008 /* Returns nonzero if it is appropriate not to emit any debugging
22009 information for BLOCK, because it doesn't contain any instructions.
22011 Don't allow this for blocks with nested functions or local classes
22012 as we would end up with orphans, and in the presence of scheduling
22013 we may end up calling them anyway. */
22015 static bool
22016 dwarf2out_ignore_block (const_tree block)
22018 tree decl;
22019 unsigned int i;
22021 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
22022 if (TREE_CODE (decl) == FUNCTION_DECL
22023 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
22024 return 0;
22025 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
22027 decl = BLOCK_NONLOCALIZED_VAR (block, i);
22028 if (TREE_CODE (decl) == FUNCTION_DECL
22029 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
22030 return 0;
22033 return 1;
22036 /* Hash table routines for file_hash. */
22038 bool
22039 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
22041 return filename_cmp (p1->filename, p2) == 0;
22044 hashval_t
22045 dwarf_file_hasher::hash (dwarf_file_data *p)
22047 return htab_hash_string (p->filename);
22050 /* Lookup FILE_NAME (in the list of filenames that we know about here in
22051 dwarf2out.c) and return its "index". The index of each (known) filename is
22052 just a unique number which is associated with only that one filename. We
22053 need such numbers for the sake of generating labels (in the .debug_sfnames
22054 section) and references to those files numbers (in the .debug_srcinfo
22055 and .debug_macinfo sections). If the filename given as an argument is not
22056 found in our current list, add it to the list and assign it the next
22057 available unique index number. */
22059 static struct dwarf_file_data *
22060 lookup_filename (const char *file_name)
22062 struct dwarf_file_data * created;
22064 if (!file_name)
22065 return NULL;
22067 dwarf_file_data **slot
22068 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
22069 INSERT);
22070 if (*slot)
22071 return *slot;
22073 created = ggc_alloc<dwarf_file_data> ();
22074 created->filename = file_name;
22075 created->emitted_number = 0;
22076 *slot = created;
22077 return created;
22080 /* If the assembler will construct the file table, then translate the compiler
22081 internal file table number into the assembler file table number, and emit
22082 a .file directive if we haven't already emitted one yet. The file table
22083 numbers are different because we prune debug info for unused variables and
22084 types, which may include filenames. */
22086 static int
22087 maybe_emit_file (struct dwarf_file_data * fd)
22089 if (! fd->emitted_number)
22091 if (last_emitted_file)
22092 fd->emitted_number = last_emitted_file->emitted_number + 1;
22093 else
22094 fd->emitted_number = 1;
22095 last_emitted_file = fd;
22097 if (DWARF2_ASM_LINE_DEBUG_INFO)
22099 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
22100 output_quoted_string (asm_out_file,
22101 remap_debug_filename (fd->filename));
22102 fputc ('\n', asm_out_file);
22106 return fd->emitted_number;
22109 /* Schedule generation of a DW_AT_const_value attribute to DIE.
22110 That generation should happen after function debug info has been
22111 generated. The value of the attribute is the constant value of ARG. */
22113 static void
22114 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
22116 die_arg_entry entry;
22118 if (!die || !arg)
22119 return;
22121 gcc_assert (early_dwarf);
22123 if (!tmpl_value_parm_die_table)
22124 vec_alloc (tmpl_value_parm_die_table, 32);
22126 entry.die = die;
22127 entry.arg = arg;
22128 vec_safe_push (tmpl_value_parm_die_table, entry);
22131 /* Return TRUE if T is an instance of generic type, FALSE
22132 otherwise. */
22134 static bool
22135 generic_type_p (tree t)
22137 if (t == NULL_TREE || !TYPE_P (t))
22138 return false;
22139 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
22142 /* Schedule the generation of the generic parameter dies for the
22143 instance of generic type T. The proper generation itself is later
22144 done by gen_scheduled_generic_parms_dies. */
22146 static void
22147 schedule_generic_params_dies_gen (tree t)
22149 if (!generic_type_p (t))
22150 return;
22152 gcc_assert (early_dwarf);
22154 if (!generic_type_instances)
22155 vec_alloc (generic_type_instances, 256);
22157 vec_safe_push (generic_type_instances, t);
22160 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
22161 by append_entry_to_tmpl_value_parm_die_table. This function must
22162 be called after function DIEs have been generated. */
22164 static void
22165 gen_remaining_tmpl_value_param_die_attribute (void)
22167 if (tmpl_value_parm_die_table)
22169 unsigned i, j;
22170 die_arg_entry *e;
22172 /* We do this in two phases - first get the cases we can
22173 handle during early-finish, preserving those we cannot
22174 (containing symbolic constants where we don't yet know
22175 whether we are going to output the referenced symbols).
22176 For those we try again at late-finish. */
22177 j = 0;
22178 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
22180 if (!tree_add_const_value_attribute (e->die, e->arg))
22181 (*tmpl_value_parm_die_table)[j++] = *e;
22183 tmpl_value_parm_die_table->truncate (j);
22187 /* Generate generic parameters DIEs for instances of generic types
22188 that have been previously scheduled by
22189 schedule_generic_params_dies_gen. This function must be called
22190 after all the types of the CU have been laid out. */
22192 static void
22193 gen_scheduled_generic_parms_dies (void)
22195 unsigned i;
22196 tree t;
22198 if (!generic_type_instances)
22199 return;
22201 /* We end up "recursing" into schedule_generic_params_dies_gen, so
22202 pretend this generation is part of "early dwarf" as well. */
22203 set_early_dwarf s;
22205 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
22206 if (COMPLETE_TYPE_P (t))
22207 gen_generic_params_dies (t);
22209 generic_type_instances = NULL;
22213 /* Replace DW_AT_name for the decl with name. */
22215 static void
22216 dwarf2out_set_name (tree decl, tree name)
22218 dw_die_ref die;
22219 dw_attr_node *attr;
22220 const char *dname;
22222 die = TYPE_SYMTAB_DIE (decl);
22223 if (!die)
22224 return;
22226 dname = dwarf2_name (name, 0);
22227 if (!dname)
22228 return;
22230 attr = get_AT (die, DW_AT_name);
22231 if (attr)
22233 struct indirect_string_node *node;
22235 node = find_AT_string (dname);
22236 /* replace the string. */
22237 attr->dw_attr_val.v.val_str = node;
22240 else
22241 add_name_attribute (die, dname);
22244 /* True if before or during processing of the first function being emitted. */
22245 static bool in_first_function_p = true;
22246 /* True if loc_note during dwarf2out_var_location call might still be
22247 before first real instruction at address equal to .Ltext0. */
22248 static bool maybe_at_text_label_p = true;
22249 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
22250 static unsigned int first_loclabel_num_not_at_text_label;
22252 /* Called by the final INSN scan whenever we see a var location. We
22253 use it to drop labels in the right places, and throw the location in
22254 our lookup table. */
22256 static void
22257 dwarf2out_var_location (rtx_insn *loc_note)
22259 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
22260 struct var_loc_node *newloc;
22261 rtx_insn *next_real, *next_note;
22262 static const char *last_label;
22263 static const char *last_postcall_label;
22264 static bool last_in_cold_section_p;
22265 static rtx_insn *expected_next_loc_note;
22266 tree decl;
22267 bool var_loc_p;
22269 if (!NOTE_P (loc_note))
22271 if (CALL_P (loc_note))
22273 call_site_count++;
22274 if (SIBLING_CALL_P (loc_note))
22275 tail_call_site_count++;
22277 return;
22280 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
22281 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
22282 return;
22284 /* Optimize processing a large consecutive sequence of location
22285 notes so we don't spend too much time in next_real_insn. If the
22286 next insn is another location note, remember the next_real_insn
22287 calculation for next time. */
22288 next_real = cached_next_real_insn;
22289 if (next_real)
22291 if (expected_next_loc_note != loc_note)
22292 next_real = NULL;
22295 next_note = NEXT_INSN (loc_note);
22296 if (! next_note
22297 || next_note->deleted ()
22298 || ! NOTE_P (next_note)
22299 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
22300 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
22301 next_note = NULL;
22303 if (! next_real)
22304 next_real = next_real_insn (loc_note);
22306 if (next_note)
22308 expected_next_loc_note = next_note;
22309 cached_next_real_insn = next_real;
22311 else
22312 cached_next_real_insn = NULL;
22314 /* If there are no instructions which would be affected by this note,
22315 don't do anything. */
22316 if (var_loc_p
22317 && next_real == NULL_RTX
22318 && !NOTE_DURING_CALL_P (loc_note))
22319 return;
22321 if (next_real == NULL_RTX)
22322 next_real = get_last_insn ();
22324 /* If there were any real insns between note we processed last time
22325 and this note (or if it is the first note), clear
22326 last_{,postcall_}label so that they are not reused this time. */
22327 if (last_var_location_insn == NULL_RTX
22328 || last_var_location_insn != next_real
22329 || last_in_cold_section_p != in_cold_section_p)
22331 last_label = NULL;
22332 last_postcall_label = NULL;
22335 if (var_loc_p)
22337 decl = NOTE_VAR_LOCATION_DECL (loc_note);
22338 newloc = add_var_loc_to_decl (decl, loc_note,
22339 NOTE_DURING_CALL_P (loc_note)
22340 ? last_postcall_label : last_label);
22341 if (newloc == NULL)
22342 return;
22344 else
22346 decl = NULL_TREE;
22347 newloc = NULL;
22350 /* If there were no real insns between note we processed last time
22351 and this note, use the label we emitted last time. Otherwise
22352 create a new label and emit it. */
22353 if (last_label == NULL)
22355 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
22356 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
22357 loclabel_num++;
22358 last_label = ggc_strdup (loclabel);
22359 /* See if loclabel might be equal to .Ltext0. If yes,
22360 bump first_loclabel_num_not_at_text_label. */
22361 if (!have_multiple_function_sections
22362 && in_first_function_p
22363 && maybe_at_text_label_p)
22365 static rtx_insn *last_start;
22366 rtx_insn *insn;
22367 for (insn = loc_note; insn; insn = previous_insn (insn))
22368 if (insn == last_start)
22369 break;
22370 else if (!NONDEBUG_INSN_P (insn))
22371 continue;
22372 else
22374 rtx body = PATTERN (insn);
22375 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
22376 continue;
22377 /* Inline asm could occupy zero bytes. */
22378 else if (GET_CODE (body) == ASM_INPUT
22379 || asm_noperands (body) >= 0)
22380 continue;
22381 #ifdef HAVE_attr_length
22382 else if (get_attr_min_length (insn) == 0)
22383 continue;
22384 #endif
22385 else
22387 /* Assume insn has non-zero length. */
22388 maybe_at_text_label_p = false;
22389 break;
22392 if (maybe_at_text_label_p)
22394 last_start = loc_note;
22395 first_loclabel_num_not_at_text_label = loclabel_num;
22400 if (!var_loc_p)
22402 struct call_arg_loc_node *ca_loc
22403 = ggc_cleared_alloc<call_arg_loc_node> ();
22404 rtx_insn *prev = prev_real_insn (loc_note);
22405 rtx x;
22406 ca_loc->call_arg_loc_note = loc_note;
22407 ca_loc->next = NULL;
22408 ca_loc->label = last_label;
22409 gcc_assert (prev
22410 && (CALL_P (prev)
22411 || (NONJUMP_INSN_P (prev)
22412 && GET_CODE (PATTERN (prev)) == SEQUENCE
22413 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
22414 if (!CALL_P (prev))
22415 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
22416 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
22417 x = get_call_rtx_from (PATTERN (prev));
22418 if (x)
22420 x = XEXP (XEXP (x, 0), 0);
22421 if (GET_CODE (x) == SYMBOL_REF
22422 && SYMBOL_REF_DECL (x)
22423 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
22424 ca_loc->symbol_ref = x;
22426 ca_loc->block = insn_scope (prev);
22427 if (call_arg_locations)
22428 call_arg_loc_last->next = ca_loc;
22429 else
22430 call_arg_locations = ca_loc;
22431 call_arg_loc_last = ca_loc;
22433 else if (!NOTE_DURING_CALL_P (loc_note))
22434 newloc->label = last_label;
22435 else
22437 if (!last_postcall_label)
22439 sprintf (loclabel, "%s-1", last_label);
22440 last_postcall_label = ggc_strdup (loclabel);
22442 newloc->label = last_postcall_label;
22445 last_var_location_insn = next_real;
22446 last_in_cold_section_p = in_cold_section_p;
22449 /* Note in one location list that text section has changed. */
22452 var_location_switch_text_section_1 (var_loc_list **slot, void *)
22454 var_loc_list *list = *slot;
22455 if (list->first)
22456 list->last_before_switch
22457 = list->last->next ? list->last->next : list->last;
22458 return 1;
22461 /* Note in all location lists that text section has changed. */
22463 static void
22464 var_location_switch_text_section (void)
22466 if (decl_loc_table == NULL)
22467 return;
22469 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
22472 /* Create a new line number table. */
22474 static dw_line_info_table *
22475 new_line_info_table (void)
22477 dw_line_info_table *table;
22479 table = ggc_cleared_alloc<dw_line_info_table> ();
22480 table->file_num = 1;
22481 table->line_num = 1;
22482 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
22484 return table;
22487 /* Lookup the "current" table into which we emit line info, so
22488 that we don't have to do it for every source line. */
22490 static void
22491 set_cur_line_info_table (section *sec)
22493 dw_line_info_table *table;
22495 if (sec == text_section)
22496 table = text_section_line_info;
22497 else if (sec == cold_text_section)
22499 table = cold_text_section_line_info;
22500 if (!table)
22502 cold_text_section_line_info = table = new_line_info_table ();
22503 table->end_label = cold_end_label;
22506 else
22508 const char *end_label;
22510 if (flag_reorder_blocks_and_partition)
22512 if (in_cold_section_p)
22513 end_label = crtl->subsections.cold_section_end_label;
22514 else
22515 end_label = crtl->subsections.hot_section_end_label;
22517 else
22519 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22520 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
22521 current_function_funcdef_no);
22522 end_label = ggc_strdup (label);
22525 table = new_line_info_table ();
22526 table->end_label = end_label;
22528 vec_safe_push (separate_line_info, table);
22531 if (DWARF2_ASM_LINE_DEBUG_INFO)
22532 table->is_stmt = (cur_line_info_table
22533 ? cur_line_info_table->is_stmt
22534 : DWARF_LINE_DEFAULT_IS_STMT_START);
22535 cur_line_info_table = table;
22539 /* We need to reset the locations at the beginning of each
22540 function. We can't do this in the end_function hook, because the
22541 declarations that use the locations won't have been output when
22542 that hook is called. Also compute have_multiple_function_sections here. */
22544 static void
22545 dwarf2out_begin_function (tree fun)
22547 section *sec = function_section (fun);
22549 if (sec != text_section)
22550 have_multiple_function_sections = true;
22552 if (flag_reorder_blocks_and_partition && !cold_text_section)
22554 gcc_assert (current_function_decl == fun);
22555 cold_text_section = unlikely_text_section ();
22556 switch_to_section (cold_text_section);
22557 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
22558 switch_to_section (sec);
22561 dwarf2out_note_section_used ();
22562 call_site_count = 0;
22563 tail_call_site_count = 0;
22565 set_cur_line_info_table (sec);
22568 /* Helper function of dwarf2out_end_function, called only after emitting
22569 the very first function into assembly. Check if some .debug_loc range
22570 might end with a .LVL* label that could be equal to .Ltext0.
22571 In that case we must force using absolute addresses in .debug_loc ranges,
22572 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
22573 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
22574 list terminator.
22575 Set have_multiple_function_sections to true in that case and
22576 terminate htab traversal. */
22579 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
22581 var_loc_list *entry = *slot;
22582 struct var_loc_node *node;
22584 node = entry->first;
22585 if (node && node->next && node->next->label)
22587 unsigned int i;
22588 const char *label = node->next->label;
22589 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
22591 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
22593 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
22594 if (strcmp (label, loclabel) == 0)
22596 have_multiple_function_sections = true;
22597 return 0;
22601 return 1;
22604 /* Hook called after emitting a function into assembly.
22605 This does something only for the very first function emitted. */
22607 static void
22608 dwarf2out_end_function (unsigned int)
22610 if (in_first_function_p
22611 && !have_multiple_function_sections
22612 && first_loclabel_num_not_at_text_label
22613 && decl_loc_table)
22614 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
22615 in_first_function_p = false;
22616 maybe_at_text_label_p = false;
22619 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
22620 front-ends register a translation unit even before dwarf2out_init is
22621 called. */
22622 static tree main_translation_unit = NULL_TREE;
22624 /* Hook called by front-ends after they built their main translation unit.
22625 Associate comp_unit_die to UNIT. */
22627 static void
22628 dwarf2out_register_main_translation_unit (tree unit)
22630 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
22631 && main_translation_unit == NULL_TREE);
22632 main_translation_unit = unit;
22633 /* If dwarf2out_init has not been called yet, it will perform the association
22634 itself looking at main_translation_unit. */
22635 if (decl_die_table != NULL)
22636 equate_decl_number_to_die (unit, comp_unit_die ());
22639 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
22641 static void
22642 push_dw_line_info_entry (dw_line_info_table *table,
22643 enum dw_line_info_opcode opcode, unsigned int val)
22645 dw_line_info_entry e;
22646 e.opcode = opcode;
22647 e.val = val;
22648 vec_safe_push (table->entries, e);
22651 /* Output a label to mark the beginning of a source code line entry
22652 and record information relating to this source line, in
22653 'line_info_table' for later output of the .debug_line section. */
22654 /* ??? The discriminator parameter ought to be unsigned. */
22656 static void
22657 dwarf2out_source_line (unsigned int line, const char *filename,
22658 int discriminator, bool is_stmt)
22660 unsigned int file_num;
22661 dw_line_info_table *table;
22663 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
22664 return;
22666 /* The discriminator column was added in dwarf4. Simplify the below
22667 by simply removing it if we're not supposed to output it. */
22668 if (dwarf_version < 4 && dwarf_strict)
22669 discriminator = 0;
22671 table = cur_line_info_table;
22672 file_num = maybe_emit_file (lookup_filename (filename));
22674 /* ??? TODO: Elide duplicate line number entries. Traditionally,
22675 the debugger has used the second (possibly duplicate) line number
22676 at the beginning of the function to mark the end of the prologue.
22677 We could eliminate any other duplicates within the function. For
22678 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
22679 that second line number entry. */
22680 /* Recall that this end-of-prologue indication is *not* the same thing
22681 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
22682 to which the hook corresponds, follows the last insn that was
22683 emitted by gen_prologue. What we need is to precede the first insn
22684 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
22685 insn that corresponds to something the user wrote. These may be
22686 very different locations once scheduling is enabled. */
22688 if (0 && file_num == table->file_num
22689 && line == table->line_num
22690 && discriminator == table->discrim_num
22691 && is_stmt == table->is_stmt)
22692 return;
22694 switch_to_section (current_function_section ());
22696 /* If requested, emit something human-readable. */
22697 if (flag_debug_asm)
22698 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
22700 if (DWARF2_ASM_LINE_DEBUG_INFO)
22702 /* Emit the .loc directive understood by GNU as. */
22703 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
22704 file_num, line, is_stmt, discriminator */
22705 fputs ("\t.loc ", asm_out_file);
22706 fprint_ul (asm_out_file, file_num);
22707 putc (' ', asm_out_file);
22708 fprint_ul (asm_out_file, line);
22709 putc (' ', asm_out_file);
22710 putc ('0', asm_out_file);
22712 if (is_stmt != table->is_stmt)
22714 fputs (" is_stmt ", asm_out_file);
22715 putc (is_stmt ? '1' : '0', asm_out_file);
22717 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22719 gcc_assert (discriminator > 0);
22720 fputs (" discriminator ", asm_out_file);
22721 fprint_ul (asm_out_file, (unsigned long) discriminator);
22723 putc ('\n', asm_out_file);
22725 else
22727 unsigned int label_num = ++line_info_label_num;
22729 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
22731 push_dw_line_info_entry (table, LI_set_address, label_num);
22732 if (file_num != table->file_num)
22733 push_dw_line_info_entry (table, LI_set_file, file_num);
22734 if (discriminator != table->discrim_num)
22735 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
22736 if (is_stmt != table->is_stmt)
22737 push_dw_line_info_entry (table, LI_negate_stmt, 0);
22738 push_dw_line_info_entry (table, LI_set_line, line);
22741 table->file_num = file_num;
22742 table->line_num = line;
22743 table->discrim_num = discriminator;
22744 table->is_stmt = is_stmt;
22745 table->in_use = true;
22748 /* Record the beginning of a new source file. */
22750 static void
22751 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22753 if (flag_eliminate_dwarf2_dups)
22755 /* Record the beginning of the file for break_out_includes. */
22756 dw_die_ref bincl_die;
22758 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22759 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22762 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22764 macinfo_entry e;
22765 e.code = DW_MACINFO_start_file;
22766 e.lineno = lineno;
22767 e.info = ggc_strdup (filename);
22768 vec_safe_push (macinfo_table, e);
22772 /* Record the end of a source file. */
22774 static void
22775 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22777 if (flag_eliminate_dwarf2_dups)
22778 /* Record the end of the file for break_out_includes. */
22779 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22781 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22783 macinfo_entry e;
22784 e.code = DW_MACINFO_end_file;
22785 e.lineno = lineno;
22786 e.info = NULL;
22787 vec_safe_push (macinfo_table, e);
22791 /* Called from debug_define in toplev.c. The `buffer' parameter contains
22792 the tail part of the directive line, i.e. the part which is past the
22793 initial whitespace, #, whitespace, directive-name, whitespace part. */
22795 static void
22796 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22797 const char *buffer ATTRIBUTE_UNUSED)
22799 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22801 macinfo_entry e;
22802 /* Insert a dummy first entry to be able to optimize the whole
22803 predefined macro block using DW_MACRO_GNU_transparent_include. */
22804 if (macinfo_table->is_empty () && lineno <= 1)
22806 e.code = 0;
22807 e.lineno = 0;
22808 e.info = NULL;
22809 vec_safe_push (macinfo_table, e);
22811 e.code = DW_MACINFO_define;
22812 e.lineno = lineno;
22813 e.info = ggc_strdup (buffer);
22814 vec_safe_push (macinfo_table, e);
22818 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
22819 the tail part of the directive line, i.e. the part which is past the
22820 initial whitespace, #, whitespace, directive-name, whitespace part. */
22822 static void
22823 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22824 const char *buffer ATTRIBUTE_UNUSED)
22826 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22828 macinfo_entry e;
22829 /* Insert a dummy first entry to be able to optimize the whole
22830 predefined macro block using DW_MACRO_GNU_transparent_include. */
22831 if (macinfo_table->is_empty () && lineno <= 1)
22833 e.code = 0;
22834 e.lineno = 0;
22835 e.info = NULL;
22836 vec_safe_push (macinfo_table, e);
22838 e.code = DW_MACINFO_undef;
22839 e.lineno = lineno;
22840 e.info = ggc_strdup (buffer);
22841 vec_safe_push (macinfo_table, e);
22845 /* Helpers to manipulate hash table of CUs. */
22847 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
22849 static inline hashval_t hash (const macinfo_entry *);
22850 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
22853 inline hashval_t
22854 macinfo_entry_hasher::hash (const macinfo_entry *entry)
22856 return htab_hash_string (entry->info);
22859 inline bool
22860 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
22861 const macinfo_entry *entry2)
22863 return !strcmp (entry1->info, entry2->info);
22866 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
22868 /* Output a single .debug_macinfo entry. */
22870 static void
22871 output_macinfo_op (macinfo_entry *ref)
22873 int file_num;
22874 size_t len;
22875 struct indirect_string_node *node;
22876 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22877 struct dwarf_file_data *fd;
22879 switch (ref->code)
22881 case DW_MACINFO_start_file:
22882 fd = lookup_filename (ref->info);
22883 file_num = maybe_emit_file (fd);
22884 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22885 dw2_asm_output_data_uleb128 (ref->lineno,
22886 "Included from line number %lu",
22887 (unsigned long) ref->lineno);
22888 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22889 break;
22890 case DW_MACINFO_end_file:
22891 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22892 break;
22893 case DW_MACINFO_define:
22894 case DW_MACINFO_undef:
22895 len = strlen (ref->info) + 1;
22896 if (!dwarf_strict
22897 && len > DWARF_OFFSET_SIZE
22898 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22899 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22901 ref->code = ref->code == DW_MACINFO_define
22902 ? DW_MACRO_GNU_define_indirect
22903 : DW_MACRO_GNU_undef_indirect;
22904 output_macinfo_op (ref);
22905 return;
22907 dw2_asm_output_data (1, ref->code,
22908 ref->code == DW_MACINFO_define
22909 ? "Define macro" : "Undefine macro");
22910 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22911 (unsigned long) ref->lineno);
22912 dw2_asm_output_nstring (ref->info, -1, "The macro");
22913 break;
22914 case DW_MACRO_GNU_define_indirect:
22915 case DW_MACRO_GNU_undef_indirect:
22916 node = find_AT_string (ref->info);
22917 gcc_assert (node
22918 && ((node->form == DW_FORM_strp)
22919 || (node->form == DW_FORM_GNU_str_index)));
22920 dw2_asm_output_data (1, ref->code,
22921 ref->code == DW_MACRO_GNU_define_indirect
22922 ? "Define macro indirect"
22923 : "Undefine macro indirect");
22924 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22925 (unsigned long) ref->lineno);
22926 if (node->form == DW_FORM_strp)
22927 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22928 debug_str_section, "The macro: \"%s\"",
22929 ref->info);
22930 else
22931 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22932 ref->info);
22933 break;
22934 case DW_MACRO_GNU_transparent_include:
22935 dw2_asm_output_data (1, ref->code, "Transparent include");
22936 ASM_GENERATE_INTERNAL_LABEL (label,
22937 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22938 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22939 break;
22940 default:
22941 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22942 ASM_COMMENT_START, (unsigned long) ref->code);
22943 break;
22947 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22948 other compilation unit .debug_macinfo sections. IDX is the first
22949 index of a define/undef, return the number of ops that should be
22950 emitted in a comdat .debug_macinfo section and emit
22951 a DW_MACRO_GNU_transparent_include entry referencing it.
22952 If the define/undef entry should be emitted normally, return 0. */
22954 static unsigned
22955 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22956 macinfo_hash_type **macinfo_htab)
22958 macinfo_entry *first, *second, *cur, *inc;
22959 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22960 unsigned char checksum[16];
22961 struct md5_ctx ctx;
22962 char *grp_name, *tail;
22963 const char *base;
22964 unsigned int i, count, encoded_filename_len, linebuf_len;
22965 macinfo_entry **slot;
22967 first = &(*macinfo_table)[idx];
22968 second = &(*macinfo_table)[idx + 1];
22970 /* Optimize only if there are at least two consecutive define/undef ops,
22971 and either all of them are before first DW_MACINFO_start_file
22972 with lineno {0,1} (i.e. predefined macro block), or all of them are
22973 in some included header file. */
22974 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22975 return 0;
22976 if (vec_safe_is_empty (files))
22978 if (first->lineno > 1 || second->lineno > 1)
22979 return 0;
22981 else if (first->lineno == 0)
22982 return 0;
22984 /* Find the last define/undef entry that can be grouped together
22985 with first and at the same time compute md5 checksum of their
22986 codes, linenumbers and strings. */
22987 md5_init_ctx (&ctx);
22988 for (i = idx; macinfo_table->iterate (i, &cur); i++)
22989 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22990 break;
22991 else if (vec_safe_is_empty (files) && cur->lineno > 1)
22992 break;
22993 else
22995 unsigned char code = cur->code;
22996 md5_process_bytes (&code, 1, &ctx);
22997 checksum_uleb128 (cur->lineno, &ctx);
22998 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
23000 md5_finish_ctx (&ctx, checksum);
23001 count = i - idx;
23003 /* From the containing include filename (if any) pick up just
23004 usable characters from its basename. */
23005 if (vec_safe_is_empty (files))
23006 base = "";
23007 else
23008 base = lbasename (files->last ().info);
23009 for (encoded_filename_len = 0, i = 0; base[i]; i++)
23010 if (ISIDNUM (base[i]) || base[i] == '.')
23011 encoded_filename_len++;
23012 /* Count . at the end. */
23013 if (encoded_filename_len)
23014 encoded_filename_len++;
23016 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
23017 linebuf_len = strlen (linebuf);
23019 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
23020 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
23021 + 16 * 2 + 1);
23022 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
23023 tail = grp_name + 4;
23024 if (encoded_filename_len)
23026 for (i = 0; base[i]; i++)
23027 if (ISIDNUM (base[i]) || base[i] == '.')
23028 *tail++ = base[i];
23029 *tail++ = '.';
23031 memcpy (tail, linebuf, linebuf_len);
23032 tail += linebuf_len;
23033 *tail++ = '.';
23034 for (i = 0; i < 16; i++)
23035 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
23037 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
23038 in the empty vector entry before the first define/undef. */
23039 inc = &(*macinfo_table)[idx - 1];
23040 inc->code = DW_MACRO_GNU_transparent_include;
23041 inc->lineno = 0;
23042 inc->info = ggc_strdup (grp_name);
23043 if (!*macinfo_htab)
23044 *macinfo_htab = new macinfo_hash_type (10);
23045 /* Avoid emitting duplicates. */
23046 slot = (*macinfo_htab)->find_slot (inc, INSERT);
23047 if (*slot != NULL)
23049 inc->code = 0;
23050 inc->info = NULL;
23051 /* If such an entry has been used before, just emit
23052 a DW_MACRO_GNU_transparent_include op. */
23053 inc = *slot;
23054 output_macinfo_op (inc);
23055 /* And clear all macinfo_entry in the range to avoid emitting them
23056 in the second pass. */
23057 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
23059 cur->code = 0;
23060 cur->info = NULL;
23063 else
23065 *slot = inc;
23066 inc->lineno = (*macinfo_htab)->elements ();
23067 output_macinfo_op (inc);
23069 return count;
23072 /* Save any strings needed by the macinfo table in the debug str
23073 table. All strings must be collected into the table by the time
23074 index_string is called. */
23076 static void
23077 save_macinfo_strings (void)
23079 unsigned len;
23080 unsigned i;
23081 macinfo_entry *ref;
23083 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
23085 switch (ref->code)
23087 /* Match the logic in output_macinfo_op to decide on
23088 indirect strings. */
23089 case DW_MACINFO_define:
23090 case DW_MACINFO_undef:
23091 len = strlen (ref->info) + 1;
23092 if (!dwarf_strict
23093 && len > DWARF_OFFSET_SIZE
23094 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
23095 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
23096 set_indirect_string (find_AT_string (ref->info));
23097 break;
23098 case DW_MACRO_GNU_define_indirect:
23099 case DW_MACRO_GNU_undef_indirect:
23100 set_indirect_string (find_AT_string (ref->info));
23101 break;
23102 default:
23103 break;
23108 /* Output macinfo section(s). */
23110 static void
23111 output_macinfo (void)
23113 unsigned i;
23114 unsigned long length = vec_safe_length (macinfo_table);
23115 macinfo_entry *ref;
23116 vec<macinfo_entry, va_gc> *files = NULL;
23117 macinfo_hash_type *macinfo_htab = NULL;
23119 if (! length)
23120 return;
23122 /* output_macinfo* uses these interchangeably. */
23123 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
23124 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
23125 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
23126 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
23128 /* For .debug_macro emit the section header. */
23129 if (!dwarf_strict)
23131 dw2_asm_output_data (2, 4, "DWARF macro version number");
23132 if (DWARF_OFFSET_SIZE == 8)
23133 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
23134 else
23135 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
23136 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
23137 (!dwarf_split_debug_info ? debug_line_section_label
23138 : debug_skeleton_line_section_label),
23139 debug_line_section, NULL);
23142 /* In the first loop, it emits the primary .debug_macinfo section
23143 and after each emitted op the macinfo_entry is cleared.
23144 If a longer range of define/undef ops can be optimized using
23145 DW_MACRO_GNU_transparent_include, the
23146 DW_MACRO_GNU_transparent_include op is emitted and kept in
23147 the vector before the first define/undef in the range and the
23148 whole range of define/undef ops is not emitted and kept. */
23149 for (i = 0; macinfo_table->iterate (i, &ref); i++)
23151 switch (ref->code)
23153 case DW_MACINFO_start_file:
23154 vec_safe_push (files, *ref);
23155 break;
23156 case DW_MACINFO_end_file:
23157 if (!vec_safe_is_empty (files))
23158 files->pop ();
23159 break;
23160 case DW_MACINFO_define:
23161 case DW_MACINFO_undef:
23162 if (!dwarf_strict
23163 && HAVE_COMDAT_GROUP
23164 && vec_safe_length (files) != 1
23165 && i > 0
23166 && i + 1 < length
23167 && (*macinfo_table)[i - 1].code == 0)
23169 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
23170 if (count)
23172 i += count - 1;
23173 continue;
23176 break;
23177 case 0:
23178 /* A dummy entry may be inserted at the beginning to be able
23179 to optimize the whole block of predefined macros. */
23180 if (i == 0)
23181 continue;
23182 default:
23183 break;
23185 output_macinfo_op (ref);
23186 ref->info = NULL;
23187 ref->code = 0;
23190 if (!macinfo_htab)
23191 return;
23193 delete macinfo_htab;
23194 macinfo_htab = NULL;
23196 /* If any DW_MACRO_GNU_transparent_include were used, on those
23197 DW_MACRO_GNU_transparent_include entries terminate the
23198 current chain and switch to a new comdat .debug_macinfo
23199 section and emit the define/undef entries within it. */
23200 for (i = 0; macinfo_table->iterate (i, &ref); i++)
23201 switch (ref->code)
23203 case 0:
23204 continue;
23205 case DW_MACRO_GNU_transparent_include:
23207 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23208 tree comdat_key = get_identifier (ref->info);
23209 /* Terminate the previous .debug_macinfo section. */
23210 dw2_asm_output_data (1, 0, "End compilation unit");
23211 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
23212 SECTION_DEBUG
23213 | SECTION_LINKONCE,
23214 comdat_key);
23215 ASM_GENERATE_INTERNAL_LABEL (label,
23216 DEBUG_MACRO_SECTION_LABEL,
23217 ref->lineno);
23218 ASM_OUTPUT_LABEL (asm_out_file, label);
23219 ref->code = 0;
23220 ref->info = NULL;
23221 dw2_asm_output_data (2, 4, "DWARF macro version number");
23222 if (DWARF_OFFSET_SIZE == 8)
23223 dw2_asm_output_data (1, 1, "Flags: 64-bit");
23224 else
23225 dw2_asm_output_data (1, 0, "Flags: 32-bit");
23227 break;
23228 case DW_MACINFO_define:
23229 case DW_MACINFO_undef:
23230 output_macinfo_op (ref);
23231 ref->code = 0;
23232 ref->info = NULL;
23233 break;
23234 default:
23235 gcc_unreachable ();
23239 /* Set up for Dwarf output at the start of compilation. */
23241 static void
23242 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
23244 /* This option is currently broken, see (PR53118 and PR46102). */
23245 if (flag_eliminate_dwarf2_dups
23246 && strstr (lang_hooks.name, "C++"))
23248 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
23249 flag_eliminate_dwarf2_dups = 0;
23252 /* Allocate the file_table. */
23253 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
23255 #ifndef DWARF2_LINENO_DEBUGGING_INFO
23256 /* Allocate the decl_die_table. */
23257 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
23259 /* Allocate the decl_loc_table. */
23260 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
23262 /* Allocate the cached_dw_loc_list_table. */
23263 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
23265 /* Allocate the initial hunk of the decl_scope_table. */
23266 vec_alloc (decl_scope_table, 256);
23268 /* Allocate the initial hunk of the abbrev_die_table. */
23269 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
23270 (ABBREV_DIE_TABLE_INCREMENT);
23271 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
23272 /* Zero-th entry is allocated, but unused. */
23273 abbrev_die_table_in_use = 1;
23275 /* Allocate the pubtypes and pubnames vectors. */
23276 vec_alloc (pubname_table, 32);
23277 vec_alloc (pubtype_table, 32);
23279 vec_alloc (incomplete_types, 64);
23281 vec_alloc (used_rtx_array, 32);
23283 if (!dwarf_split_debug_info)
23285 debug_info_section = get_section (DEBUG_INFO_SECTION,
23286 SECTION_DEBUG, NULL);
23287 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23288 SECTION_DEBUG, NULL);
23289 debug_loc_section = get_section (DEBUG_LOC_SECTION,
23290 SECTION_DEBUG, NULL);
23292 else
23294 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
23295 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23296 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
23297 SECTION_DEBUG | SECTION_EXCLUDE,
23298 NULL);
23299 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
23300 SECTION_DEBUG, NULL);
23301 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
23302 SECTION_DEBUG, NULL);
23303 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23304 SECTION_DEBUG, NULL);
23305 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
23306 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
23308 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
23309 the main .o, but the skeleton_line goes into the split off dwo. */
23310 debug_skeleton_line_section
23311 = get_section (DEBUG_DWO_LINE_SECTION,
23312 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23313 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
23314 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
23315 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
23316 SECTION_DEBUG | SECTION_EXCLUDE,
23317 NULL);
23318 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
23319 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
23320 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
23321 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23322 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
23323 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
23325 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
23326 SECTION_DEBUG, NULL);
23327 debug_macinfo_section = get_section (dwarf_strict
23328 ? DEBUG_MACINFO_SECTION
23329 : DEBUG_MACRO_SECTION,
23330 DEBUG_MACRO_SECTION_FLAGS, NULL);
23331 debug_line_section = get_section (DEBUG_LINE_SECTION,
23332 SECTION_DEBUG, NULL);
23333 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
23334 SECTION_DEBUG, NULL);
23335 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
23336 SECTION_DEBUG, NULL);
23337 debug_str_section = get_section (DEBUG_STR_SECTION,
23338 DEBUG_STR_SECTION_FLAGS, NULL);
23339 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
23340 SECTION_DEBUG, NULL);
23341 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
23342 SECTION_DEBUG, NULL);
23344 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
23345 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
23346 DEBUG_ABBREV_SECTION_LABEL, 0);
23347 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
23348 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
23349 COLD_TEXT_SECTION_LABEL, 0);
23350 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
23352 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
23353 DEBUG_INFO_SECTION_LABEL, 0);
23354 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
23355 DEBUG_LINE_SECTION_LABEL, 0);
23356 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
23357 DEBUG_RANGES_SECTION_LABEL, 0);
23358 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
23359 DEBUG_ADDR_SECTION_LABEL, 0);
23360 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
23361 dwarf_strict
23362 ? DEBUG_MACINFO_SECTION_LABEL
23363 : DEBUG_MACRO_SECTION_LABEL, 0);
23364 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
23366 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23367 vec_alloc (macinfo_table, 64);
23369 switch_to_section (text_section);
23370 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
23371 #endif
23373 /* Make sure the line number table for .text always exists. */
23374 text_section_line_info = new_line_info_table ();
23375 text_section_line_info->end_label = text_end_label;
23377 #ifdef DWARF2_LINENO_DEBUGGING_INFO
23378 cur_line_info_table = text_section_line_info;
23379 #endif
23381 /* If front-ends already registered a main translation unit but we were not
23382 ready to perform the association, do this now. */
23383 if (main_translation_unit != NULL_TREE)
23384 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
23387 /* Called before compile () starts outputtting functions, variables
23388 and toplevel asms into assembly. */
23390 static void
23391 dwarf2out_assembly_start (void)
23393 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
23394 && dwarf2out_do_cfi_asm ()
23395 && (!(flag_unwind_tables || flag_exceptions)
23396 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
23397 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
23400 /* A helper function for dwarf2out_finish called through
23401 htab_traverse. Assign a string its index. All strings must be
23402 collected into the table by the time index_string is called,
23403 because the indexing code relies on htab_traverse to traverse nodes
23404 in the same order for each run. */
23407 index_string (indirect_string_node **h, unsigned int *index)
23409 indirect_string_node *node = *h;
23411 find_string_form (node);
23412 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23414 gcc_assert (node->index == NO_INDEX_ASSIGNED);
23415 node->index = *index;
23416 *index += 1;
23418 return 1;
23421 /* A helper function for output_indirect_strings called through
23422 htab_traverse. Output the offset to a string and update the
23423 current offset. */
23426 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
23428 indirect_string_node *node = *h;
23430 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23432 /* Assert that this node has been assigned an index. */
23433 gcc_assert (node->index != NO_INDEX_ASSIGNED
23434 && node->index != NOT_INDEXED);
23435 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
23436 "indexed string 0x%x: %s", node->index, node->str);
23437 *offset += strlen (node->str) + 1;
23439 return 1;
23442 /* A helper function for dwarf2out_finish called through
23443 htab_traverse. Output the indexed string. */
23446 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
23448 struct indirect_string_node *node = *h;
23450 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23452 /* Assert that the strings are output in the same order as their
23453 indexes were assigned. */
23454 gcc_assert (*cur_idx == node->index);
23455 assemble_string (node->str, strlen (node->str) + 1);
23456 *cur_idx += 1;
23458 return 1;
23461 /* A helper function for dwarf2out_finish called through
23462 htab_traverse. Emit one queued .debug_str string. */
23465 output_indirect_string (indirect_string_node **h, void *)
23467 struct indirect_string_node *node = *h;
23469 node->form = find_string_form (node);
23470 if (node->form == DW_FORM_strp && node->refcount > 0)
23472 ASM_OUTPUT_LABEL (asm_out_file, node->label);
23473 assemble_string (node->str, strlen (node->str) + 1);
23476 return 1;
23479 /* Output the indexed string table. */
23481 static void
23482 output_indirect_strings (void)
23484 switch_to_section (debug_str_section);
23485 if (!dwarf_split_debug_info)
23486 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
23487 else
23489 unsigned int offset = 0;
23490 unsigned int cur_idx = 0;
23492 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
23494 switch_to_section (debug_str_offsets_section);
23495 debug_str_hash->traverse_noresize
23496 <unsigned int *, output_index_string_offset> (&offset);
23497 switch_to_section (debug_str_dwo_section);
23498 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
23499 (&cur_idx);
23503 /* Callback for htab_traverse to assign an index to an entry in the
23504 table, and to write that entry to the .debug_addr section. */
23507 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
23509 addr_table_entry *entry = *slot;
23511 if (entry->refcount == 0)
23513 gcc_assert (entry->index == NO_INDEX_ASSIGNED
23514 || entry->index == NOT_INDEXED);
23515 return 1;
23518 gcc_assert (entry->index == *cur_index);
23519 (*cur_index)++;
23521 switch (entry->kind)
23523 case ate_kind_rtx:
23524 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
23525 "0x%x", entry->index);
23526 break;
23527 case ate_kind_rtx_dtprel:
23528 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
23529 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
23530 DWARF2_ADDR_SIZE,
23531 entry->addr.rtl);
23532 fputc ('\n', asm_out_file);
23533 break;
23534 case ate_kind_label:
23535 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
23536 "0x%x", entry->index);
23537 break;
23538 default:
23539 gcc_unreachable ();
23541 return 1;
23544 /* Produce the .debug_addr section. */
23546 static void
23547 output_addr_table (void)
23549 unsigned int index = 0;
23550 if (addr_index_table == NULL || addr_index_table->size () == 0)
23551 return;
23553 switch_to_section (debug_addr_section);
23554 addr_index_table
23555 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
23558 #if ENABLE_ASSERT_CHECKING
23559 /* Verify that all marks are clear. */
23561 static void
23562 verify_marks_clear (dw_die_ref die)
23564 dw_die_ref c;
23566 gcc_assert (! die->die_mark);
23567 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
23569 #endif /* ENABLE_ASSERT_CHECKING */
23571 /* Clear the marks for a die and its children.
23572 Be cool if the mark isn't set. */
23574 static void
23575 prune_unmark_dies (dw_die_ref die)
23577 dw_die_ref c;
23579 if (die->die_mark)
23580 die->die_mark = 0;
23581 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
23584 /* Given DIE that we're marking as used, find any other dies
23585 it references as attributes and mark them as used. */
23587 static void
23588 prune_unused_types_walk_attribs (dw_die_ref die)
23590 dw_attr_node *a;
23591 unsigned ix;
23593 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23595 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
23597 /* A reference to another DIE.
23598 Make sure that it will get emitted.
23599 If it was broken out into a comdat group, don't follow it. */
23600 if (! AT_ref (a)->comdat_type_p
23601 || a->dw_attr == DW_AT_specification)
23602 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
23604 /* Set the string's refcount to 0 so that prune_unused_types_mark
23605 accounts properly for it. */
23606 if (AT_class (a) == dw_val_class_str)
23607 a->dw_attr_val.v.val_str->refcount = 0;
23611 /* Mark the generic parameters and arguments children DIEs of DIE. */
23613 static void
23614 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
23616 dw_die_ref c;
23618 if (die == NULL || die->die_child == NULL)
23619 return;
23620 c = die->die_child;
23623 if (is_template_parameter (c))
23624 prune_unused_types_mark (c, 1);
23625 c = c->die_sib;
23626 } while (c && c != die->die_child);
23629 /* Mark DIE as being used. If DOKIDS is true, then walk down
23630 to DIE's children. */
23632 static void
23633 prune_unused_types_mark (dw_die_ref die, int dokids)
23635 dw_die_ref c;
23637 if (die->die_mark == 0)
23639 /* We haven't done this node yet. Mark it as used. */
23640 die->die_mark = 1;
23641 /* If this is the DIE of a generic type instantiation,
23642 mark the children DIEs that describe its generic parms and
23643 args. */
23644 prune_unused_types_mark_generic_parms_dies (die);
23646 /* We also have to mark its parents as used.
23647 (But we don't want to mark our parent's kids due to this,
23648 unless it is a class.) */
23649 if (die->die_parent)
23650 prune_unused_types_mark (die->die_parent,
23651 class_scope_p (die->die_parent));
23653 /* Mark any referenced nodes. */
23654 prune_unused_types_walk_attribs (die);
23656 /* If this node is a specification,
23657 also mark the definition, if it exists. */
23658 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
23659 prune_unused_types_mark (die->die_definition, 1);
23662 if (dokids && die->die_mark != 2)
23664 /* We need to walk the children, but haven't done so yet.
23665 Remember that we've walked the kids. */
23666 die->die_mark = 2;
23668 /* If this is an array type, we need to make sure our
23669 kids get marked, even if they're types. If we're
23670 breaking out types into comdat sections, do this
23671 for all type definitions. */
23672 if (die->die_tag == DW_TAG_array_type
23673 || (use_debug_types
23674 && is_type_die (die) && ! is_declaration_die (die)))
23675 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
23676 else
23677 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23681 /* For local classes, look if any static member functions were emitted
23682 and if so, mark them. */
23684 static void
23685 prune_unused_types_walk_local_classes (dw_die_ref die)
23687 dw_die_ref c;
23689 if (die->die_mark == 2)
23690 return;
23692 switch (die->die_tag)
23694 case DW_TAG_structure_type:
23695 case DW_TAG_union_type:
23696 case DW_TAG_class_type:
23697 break;
23699 case DW_TAG_subprogram:
23700 if (!get_AT_flag (die, DW_AT_declaration)
23701 || die->die_definition != NULL)
23702 prune_unused_types_mark (die, 1);
23703 return;
23705 default:
23706 return;
23709 /* Mark children. */
23710 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
23713 /* Walk the tree DIE and mark types that we actually use. */
23715 static void
23716 prune_unused_types_walk (dw_die_ref die)
23718 dw_die_ref c;
23720 /* Don't do anything if this node is already marked and
23721 children have been marked as well. */
23722 if (die->die_mark == 2)
23723 return;
23725 switch (die->die_tag)
23727 case DW_TAG_structure_type:
23728 case DW_TAG_union_type:
23729 case DW_TAG_class_type:
23730 if (die->die_perennial_p)
23731 break;
23733 for (c = die->die_parent; c; c = c->die_parent)
23734 if (c->die_tag == DW_TAG_subprogram)
23735 break;
23737 /* Finding used static member functions inside of classes
23738 is needed just for local classes, because for other classes
23739 static member function DIEs with DW_AT_specification
23740 are emitted outside of the DW_TAG_*_type. If we ever change
23741 it, we'd need to call this even for non-local classes. */
23742 if (c)
23743 prune_unused_types_walk_local_classes (die);
23745 /* It's a type node --- don't mark it. */
23746 return;
23748 case DW_TAG_const_type:
23749 case DW_TAG_packed_type:
23750 case DW_TAG_pointer_type:
23751 case DW_TAG_reference_type:
23752 case DW_TAG_rvalue_reference_type:
23753 case DW_TAG_volatile_type:
23754 case DW_TAG_typedef:
23755 case DW_TAG_array_type:
23756 case DW_TAG_interface_type:
23757 case DW_TAG_friend:
23758 case DW_TAG_variant_part:
23759 case DW_TAG_enumeration_type:
23760 case DW_TAG_subroutine_type:
23761 case DW_TAG_string_type:
23762 case DW_TAG_set_type:
23763 case DW_TAG_subrange_type:
23764 case DW_TAG_ptr_to_member_type:
23765 case DW_TAG_file_type:
23766 if (die->die_perennial_p)
23767 break;
23769 /* It's a type node --- don't mark it. */
23770 return;
23772 default:
23773 /* Mark everything else. */
23774 break;
23777 if (die->die_mark == 0)
23779 die->die_mark = 1;
23781 /* Now, mark any dies referenced from here. */
23782 prune_unused_types_walk_attribs (die);
23785 die->die_mark = 2;
23787 /* Mark children. */
23788 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23791 /* Increment the string counts on strings referred to from DIE's
23792 attributes. */
23794 static void
23795 prune_unused_types_update_strings (dw_die_ref die)
23797 dw_attr_node *a;
23798 unsigned ix;
23800 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23801 if (AT_class (a) == dw_val_class_str)
23803 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
23804 s->refcount++;
23805 /* Avoid unnecessarily putting strings that are used less than
23806 twice in the hash table. */
23807 if (s->refcount
23808 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
23810 indirect_string_node **slot
23811 = debug_str_hash->find_slot_with_hash (s->str,
23812 htab_hash_string (s->str),
23813 INSERT);
23814 gcc_assert (*slot == NULL);
23815 *slot = s;
23820 /* Remove from the tree DIE any dies that aren't marked. */
23822 static void
23823 prune_unused_types_prune (dw_die_ref die)
23825 dw_die_ref c;
23827 gcc_assert (die->die_mark);
23828 prune_unused_types_update_strings (die);
23830 if (! die->die_child)
23831 return;
23833 c = die->die_child;
23834 do {
23835 dw_die_ref prev = c;
23836 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23837 if (c == die->die_child)
23839 /* No marked children between 'prev' and the end of the list. */
23840 if (prev == c)
23841 /* No marked children at all. */
23842 die->die_child = NULL;
23843 else
23845 prev->die_sib = c->die_sib;
23846 die->die_child = prev;
23848 return;
23851 if (c != prev->die_sib)
23852 prev->die_sib = c;
23853 prune_unused_types_prune (c);
23854 } while (c != die->die_child);
23857 /* Remove dies representing declarations that we never use. */
23859 static void
23860 prune_unused_types (void)
23862 unsigned int i;
23863 limbo_die_node *node;
23864 comdat_type_node *ctnode;
23865 pubname_entry *pub;
23866 dw_die_ref base_type;
23868 #if ENABLE_ASSERT_CHECKING
23869 /* All the marks should already be clear. */
23870 verify_marks_clear (comp_unit_die ());
23871 for (node = limbo_die_list; node; node = node->next)
23872 verify_marks_clear (node->die);
23873 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23874 verify_marks_clear (ctnode->root_die);
23875 #endif /* ENABLE_ASSERT_CHECKING */
23877 /* Mark types that are used in global variables. */
23878 premark_types_used_by_global_vars ();
23880 /* Set the mark on nodes that are actually used. */
23881 prune_unused_types_walk (comp_unit_die ());
23882 for (node = limbo_die_list; node; node = node->next)
23883 prune_unused_types_walk (node->die);
23884 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23886 prune_unused_types_walk (ctnode->root_die);
23887 prune_unused_types_mark (ctnode->type_die, 1);
23890 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
23891 are unusual in that they are pubnames that are the children of pubtypes.
23892 They should only be marked via their parent DW_TAG_enumeration_type die,
23893 not as roots in themselves. */
23894 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
23895 if (pub->die->die_tag != DW_TAG_enumerator)
23896 prune_unused_types_mark (pub->die, 1);
23897 for (i = 0; base_types.iterate (i, &base_type); i++)
23898 prune_unused_types_mark (base_type, 1);
23900 if (debug_str_hash)
23901 debug_str_hash->empty ();
23902 if (skeleton_debug_str_hash)
23903 skeleton_debug_str_hash->empty ();
23904 prune_unused_types_prune (comp_unit_die ());
23905 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
23907 node = *pnode;
23908 if (!node->die->die_mark)
23909 *pnode = node->next;
23910 else
23912 prune_unused_types_prune (node->die);
23913 pnode = &node->next;
23916 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23917 prune_unused_types_prune (ctnode->root_die);
23919 /* Leave the marks clear. */
23920 prune_unmark_dies (comp_unit_die ());
23921 for (node = limbo_die_list; node; node = node->next)
23922 prune_unmark_dies (node->die);
23923 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23924 prune_unmark_dies (ctnode->root_die);
23927 /* Set the parameter to true if there are any relative pathnames in
23928 the file table. */
23930 file_table_relative_p (dwarf_file_data **slot, bool *p)
23932 struct dwarf_file_data *d = *slot;
23933 if (!IS_ABSOLUTE_PATH (d->filename))
23935 *p = true;
23936 return 0;
23938 return 1;
23941 /* Helpers to manipulate hash table of comdat type units. */
23943 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
23945 static inline hashval_t hash (const comdat_type_node *);
23946 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
23949 inline hashval_t
23950 comdat_type_hasher::hash (const comdat_type_node *type_node)
23952 hashval_t h;
23953 memcpy (&h, type_node->signature, sizeof (h));
23954 return h;
23957 inline bool
23958 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
23959 const comdat_type_node *type_node_2)
23961 return (! memcmp (type_node_1->signature, type_node_2->signature,
23962 DWARF_TYPE_SIGNATURE_SIZE));
23965 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23966 to the location it would have been added, should we know its
23967 DECL_ASSEMBLER_NAME when we added other attributes. This will
23968 probably improve compactness of debug info, removing equivalent
23969 abbrevs, and hide any differences caused by deferring the
23970 computation of the assembler name, triggered by e.g. PCH. */
23972 static inline void
23973 move_linkage_attr (dw_die_ref die)
23975 unsigned ix = vec_safe_length (die->die_attr);
23976 dw_attr_node linkage = (*die->die_attr)[ix - 1];
23978 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23979 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23981 while (--ix > 0)
23983 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23985 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23986 break;
23989 if (ix != vec_safe_length (die->die_attr) - 1)
23991 die->die_attr->pop ();
23992 die->die_attr->quick_insert (ix, linkage);
23996 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23997 referenced from typed stack ops and count how often they are used. */
23999 static void
24000 mark_base_types (dw_loc_descr_ref loc)
24002 dw_die_ref base_type = NULL;
24004 for (; loc; loc = loc->dw_loc_next)
24006 switch (loc->dw_loc_opc)
24008 case DW_OP_GNU_regval_type:
24009 case DW_OP_GNU_deref_type:
24010 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
24011 break;
24012 case DW_OP_GNU_convert:
24013 case DW_OP_GNU_reinterpret:
24014 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
24015 continue;
24016 /* FALLTHRU */
24017 case DW_OP_GNU_const_type:
24018 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
24019 break;
24020 case DW_OP_GNU_entry_value:
24021 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
24022 continue;
24023 default:
24024 continue;
24026 gcc_assert (base_type->die_parent == comp_unit_die ());
24027 if (base_type->die_mark)
24028 base_type->die_mark++;
24029 else
24031 base_types.safe_push (base_type);
24032 base_type->die_mark = 1;
24037 /* Comparison function for sorting marked base types. */
24039 static int
24040 base_type_cmp (const void *x, const void *y)
24042 dw_die_ref dx = *(const dw_die_ref *) x;
24043 dw_die_ref dy = *(const dw_die_ref *) y;
24044 unsigned int byte_size1, byte_size2;
24045 unsigned int encoding1, encoding2;
24046 if (dx->die_mark > dy->die_mark)
24047 return -1;
24048 if (dx->die_mark < dy->die_mark)
24049 return 1;
24050 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
24051 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
24052 if (byte_size1 < byte_size2)
24053 return 1;
24054 if (byte_size1 > byte_size2)
24055 return -1;
24056 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
24057 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
24058 if (encoding1 < encoding2)
24059 return 1;
24060 if (encoding1 > encoding2)
24061 return -1;
24062 return 0;
24065 /* Move base types marked by mark_base_types as early as possible
24066 in the CU, sorted by decreasing usage count both to make the
24067 uleb128 references as small as possible and to make sure they
24068 will have die_offset already computed by calc_die_sizes when
24069 sizes of typed stack loc ops is computed. */
24071 static void
24072 move_marked_base_types (void)
24074 unsigned int i;
24075 dw_die_ref base_type, die, c;
24077 if (base_types.is_empty ())
24078 return;
24080 /* Sort by decreasing usage count, they will be added again in that
24081 order later on. */
24082 base_types.qsort (base_type_cmp);
24083 die = comp_unit_die ();
24084 c = die->die_child;
24087 dw_die_ref prev = c;
24088 c = c->die_sib;
24089 while (c->die_mark)
24091 remove_child_with_prev (c, prev);
24092 /* As base types got marked, there must be at least
24093 one node other than DW_TAG_base_type. */
24094 gcc_assert (c != c->die_sib);
24095 c = c->die_sib;
24098 while (c != die->die_child);
24099 gcc_assert (die->die_child);
24100 c = die->die_child;
24101 for (i = 0; base_types.iterate (i, &base_type); i++)
24103 base_type->die_mark = 0;
24104 base_type->die_sib = c->die_sib;
24105 c->die_sib = base_type;
24106 c = base_type;
24110 /* Helper function for resolve_addr, attempt to resolve
24111 one CONST_STRING, return true if successful. Similarly verify that
24112 SYMBOL_REFs refer to variables emitted in the current CU. */
24114 static bool
24115 resolve_one_addr (rtx *addr)
24117 rtx rtl = *addr;
24119 if (GET_CODE (rtl) == CONST_STRING)
24121 size_t len = strlen (XSTR (rtl, 0)) + 1;
24122 tree t = build_string (len, XSTR (rtl, 0));
24123 tree tlen = size_int (len - 1);
24124 TREE_TYPE (t)
24125 = build_array_type (char_type_node, build_index_type (tlen));
24126 rtl = lookup_constant_def (t);
24127 if (!rtl || !MEM_P (rtl))
24128 return false;
24129 rtl = XEXP (rtl, 0);
24130 if (GET_CODE (rtl) == SYMBOL_REF
24131 && SYMBOL_REF_DECL (rtl)
24132 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
24133 return false;
24134 vec_safe_push (used_rtx_array, rtl);
24135 *addr = rtl;
24136 return true;
24139 if (GET_CODE (rtl) == SYMBOL_REF
24140 && SYMBOL_REF_DECL (rtl))
24142 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
24144 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
24145 return false;
24147 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
24148 return false;
24151 if (GET_CODE (rtl) == CONST)
24153 subrtx_ptr_iterator::array_type array;
24154 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
24155 if (!resolve_one_addr (*iter))
24156 return false;
24159 return true;
24162 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
24163 if possible, and create DW_TAG_dwarf_procedure that can be referenced
24164 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
24166 static rtx
24167 string_cst_pool_decl (tree t)
24169 rtx rtl = output_constant_def (t, 1);
24170 unsigned char *array;
24171 dw_loc_descr_ref l;
24172 tree decl;
24173 size_t len;
24174 dw_die_ref ref;
24176 if (!rtl || !MEM_P (rtl))
24177 return NULL_RTX;
24178 rtl = XEXP (rtl, 0);
24179 if (GET_CODE (rtl) != SYMBOL_REF
24180 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
24181 return NULL_RTX;
24183 decl = SYMBOL_REF_DECL (rtl);
24184 if (!lookup_decl_die (decl))
24186 len = TREE_STRING_LENGTH (t);
24187 vec_safe_push (used_rtx_array, rtl);
24188 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
24189 array = ggc_vec_alloc<unsigned char> (len);
24190 memcpy (array, TREE_STRING_POINTER (t), len);
24191 l = new_loc_descr (DW_OP_implicit_value, len, 0);
24192 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
24193 l->dw_loc_oprnd2.v.val_vec.length = len;
24194 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
24195 l->dw_loc_oprnd2.v.val_vec.array = array;
24196 add_AT_loc (ref, DW_AT_location, l);
24197 equate_decl_number_to_die (decl, ref);
24199 return rtl;
24202 /* Helper function of resolve_addr_in_expr. LOC is
24203 a DW_OP_addr followed by DW_OP_stack_value, either at the start
24204 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
24205 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
24206 with DW_OP_GNU_implicit_pointer if possible
24207 and return true, if unsuccessful, return false. */
24209 static bool
24210 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
24212 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
24213 HOST_WIDE_INT offset = 0;
24214 dw_die_ref ref = NULL;
24215 tree decl;
24217 if (GET_CODE (rtl) == CONST
24218 && GET_CODE (XEXP (rtl, 0)) == PLUS
24219 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
24221 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
24222 rtl = XEXP (XEXP (rtl, 0), 0);
24224 if (GET_CODE (rtl) == CONST_STRING)
24226 size_t len = strlen (XSTR (rtl, 0)) + 1;
24227 tree t = build_string (len, XSTR (rtl, 0));
24228 tree tlen = size_int (len - 1);
24230 TREE_TYPE (t)
24231 = build_array_type (char_type_node, build_index_type (tlen));
24232 rtl = string_cst_pool_decl (t);
24233 if (!rtl)
24234 return false;
24236 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
24238 decl = SYMBOL_REF_DECL (rtl);
24239 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
24241 ref = lookup_decl_die (decl);
24242 if (ref && (get_AT (ref, DW_AT_location)
24243 || get_AT (ref, DW_AT_const_value)))
24245 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
24246 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24247 loc->dw_loc_oprnd1.val_entry = NULL;
24248 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
24249 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
24250 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
24251 loc->dw_loc_oprnd2.v.val_int = offset;
24252 return true;
24256 return false;
24259 /* Helper function for resolve_addr, handle one location
24260 expression, return false if at least one CONST_STRING or SYMBOL_REF in
24261 the location list couldn't be resolved. */
24263 static bool
24264 resolve_addr_in_expr (dw_loc_descr_ref loc)
24266 dw_loc_descr_ref keep = NULL;
24267 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
24268 switch (loc->dw_loc_opc)
24270 case DW_OP_addr:
24271 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
24273 if ((prev == NULL
24274 || prev->dw_loc_opc == DW_OP_piece
24275 || prev->dw_loc_opc == DW_OP_bit_piece)
24276 && loc->dw_loc_next
24277 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
24278 && !dwarf_strict
24279 && optimize_one_addr_into_implicit_ptr (loc))
24280 break;
24281 return false;
24283 break;
24284 case DW_OP_GNU_addr_index:
24285 case DW_OP_GNU_const_index:
24286 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
24287 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
24289 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
24290 if (!resolve_one_addr (&rtl))
24291 return false;
24292 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
24293 loc->dw_loc_oprnd1.val_entry =
24294 add_addr_table_entry (rtl, ate_kind_rtx);
24296 break;
24297 case DW_OP_const4u:
24298 case DW_OP_const8u:
24299 if (loc->dtprel
24300 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
24301 return false;
24302 break;
24303 case DW_OP_plus_uconst:
24304 if (size_of_loc_descr (loc)
24305 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
24307 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
24309 dw_loc_descr_ref repl
24310 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
24311 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
24312 add_loc_descr (&repl, loc->dw_loc_next);
24313 *loc = *repl;
24315 break;
24316 case DW_OP_implicit_value:
24317 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
24318 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
24319 return false;
24320 break;
24321 case DW_OP_GNU_implicit_pointer:
24322 case DW_OP_GNU_parameter_ref:
24323 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
24325 dw_die_ref ref
24326 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
24327 if (ref == NULL)
24328 return false;
24329 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24330 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
24331 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
24333 break;
24334 case DW_OP_GNU_const_type:
24335 case DW_OP_GNU_regval_type:
24336 case DW_OP_GNU_deref_type:
24337 case DW_OP_GNU_convert:
24338 case DW_OP_GNU_reinterpret:
24339 while (loc->dw_loc_next
24340 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
24342 dw_die_ref base1, base2;
24343 unsigned enc1, enc2, size1, size2;
24344 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24345 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24346 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
24347 else if (loc->dw_loc_oprnd1.val_class
24348 == dw_val_class_unsigned_const)
24349 break;
24350 else
24351 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
24352 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
24353 == dw_val_class_unsigned_const)
24354 break;
24355 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
24356 gcc_assert (base1->die_tag == DW_TAG_base_type
24357 && base2->die_tag == DW_TAG_base_type);
24358 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
24359 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
24360 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
24361 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
24362 if (size1 == size2
24363 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
24364 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
24365 && loc != keep)
24366 || enc1 == enc2))
24368 /* Optimize away next DW_OP_GNU_convert after
24369 adjusting LOC's base type die reference. */
24370 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24371 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24372 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
24373 else
24374 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
24375 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
24376 continue;
24378 /* Don't change integer DW_OP_GNU_convert after e.g. floating
24379 point typed stack entry. */
24380 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
24381 keep = loc->dw_loc_next;
24382 break;
24384 break;
24385 default:
24386 break;
24388 return true;
24391 /* Helper function of resolve_addr. DIE had DW_AT_location of
24392 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
24393 and DW_OP_addr couldn't be resolved. resolve_addr has already
24394 removed the DW_AT_location attribute. This function attempts to
24395 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
24396 to it or DW_AT_const_value attribute, if possible. */
24398 static void
24399 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
24401 if (TREE_CODE (decl) != VAR_DECL
24402 || lookup_decl_die (decl) != die
24403 || DECL_EXTERNAL (decl)
24404 || !TREE_STATIC (decl)
24405 || DECL_INITIAL (decl) == NULL_TREE
24406 || DECL_P (DECL_INITIAL (decl))
24407 || get_AT (die, DW_AT_const_value))
24408 return;
24410 tree init = DECL_INITIAL (decl);
24411 HOST_WIDE_INT offset = 0;
24412 /* For variables that have been optimized away and thus
24413 don't have a memory location, see if we can emit
24414 DW_AT_const_value instead. */
24415 if (tree_add_const_value_attribute (die, init))
24416 return;
24417 if (dwarf_strict)
24418 return;
24419 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
24420 and ADDR_EXPR refers to a decl that has DW_AT_location or
24421 DW_AT_const_value (but isn't addressable, otherwise
24422 resolving the original DW_OP_addr wouldn't fail), see if
24423 we can add DW_OP_GNU_implicit_pointer. */
24424 STRIP_NOPS (init);
24425 if (TREE_CODE (init) == POINTER_PLUS_EXPR
24426 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
24428 offset = tree_to_shwi (TREE_OPERAND (init, 1));
24429 init = TREE_OPERAND (init, 0);
24430 STRIP_NOPS (init);
24432 if (TREE_CODE (init) != ADDR_EXPR)
24433 return;
24434 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
24435 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
24436 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
24437 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
24438 && TREE_OPERAND (init, 0) != decl))
24440 dw_die_ref ref;
24441 dw_loc_descr_ref l;
24443 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
24445 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
24446 if (!rtl)
24447 return;
24448 decl = SYMBOL_REF_DECL (rtl);
24450 else
24451 decl = TREE_OPERAND (init, 0);
24452 ref = lookup_decl_die (decl);
24453 if (ref == NULL
24454 || (!get_AT (ref, DW_AT_location)
24455 && !get_AT (ref, DW_AT_const_value)))
24456 return;
24457 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
24458 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24459 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
24460 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
24461 add_AT_loc (die, DW_AT_location, l);
24465 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
24466 an address in .rodata section if the string literal is emitted there,
24467 or remove the containing location list or replace DW_AT_const_value
24468 with DW_AT_location and empty location expression, if it isn't found
24469 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
24470 to something that has been emitted in the current CU. */
24472 static void
24473 resolve_addr (dw_die_ref die)
24475 dw_die_ref c;
24476 dw_attr_node *a;
24477 dw_loc_list_ref *curr, *start, loc;
24478 unsigned ix;
24480 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24481 switch (AT_class (a))
24483 case dw_val_class_loc_list:
24484 start = curr = AT_loc_list_ptr (a);
24485 loc = *curr;
24486 gcc_assert (loc);
24487 /* The same list can be referenced more than once. See if we have
24488 already recorded the result from a previous pass. */
24489 if (loc->replaced)
24490 *curr = loc->dw_loc_next;
24491 else if (!loc->resolved_addr)
24493 /* As things stand, we do not expect or allow one die to
24494 reference a suffix of another die's location list chain.
24495 References must be identical or completely separate.
24496 There is therefore no need to cache the result of this
24497 pass on any list other than the first; doing so
24498 would lead to unnecessary writes. */
24499 while (*curr)
24501 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
24502 if (!resolve_addr_in_expr ((*curr)->expr))
24504 dw_loc_list_ref next = (*curr)->dw_loc_next;
24505 dw_loc_descr_ref l = (*curr)->expr;
24507 if (next && (*curr)->ll_symbol)
24509 gcc_assert (!next->ll_symbol);
24510 next->ll_symbol = (*curr)->ll_symbol;
24512 if (dwarf_split_debug_info)
24513 remove_loc_list_addr_table_entries (l);
24514 *curr = next;
24516 else
24518 mark_base_types ((*curr)->expr);
24519 curr = &(*curr)->dw_loc_next;
24522 if (loc == *start)
24523 loc->resolved_addr = 1;
24524 else
24526 loc->replaced = 1;
24527 loc->dw_loc_next = *start;
24530 if (!*start)
24532 remove_AT (die, a->dw_attr);
24533 ix--;
24535 break;
24536 case dw_val_class_loc:
24538 dw_loc_descr_ref l = AT_loc (a);
24539 /* For -gdwarf-2 don't attempt to optimize
24540 DW_AT_data_member_location containing
24541 DW_OP_plus_uconst - older consumers might
24542 rely on it being that op instead of a more complex,
24543 but shorter, location description. */
24544 if ((dwarf_version > 2
24545 || a->dw_attr != DW_AT_data_member_location
24546 || l == NULL
24547 || l->dw_loc_opc != DW_OP_plus_uconst
24548 || l->dw_loc_next != NULL)
24549 && !resolve_addr_in_expr (l))
24551 if (dwarf_split_debug_info)
24552 remove_loc_list_addr_table_entries (l);
24553 if (l != NULL
24554 && l->dw_loc_next == NULL
24555 && l->dw_loc_opc == DW_OP_addr
24556 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
24557 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
24558 && a->dw_attr == DW_AT_location)
24560 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
24561 remove_AT (die, a->dw_attr);
24562 ix--;
24563 optimize_location_into_implicit_ptr (die, decl);
24564 break;
24566 remove_AT (die, a->dw_attr);
24567 ix--;
24569 else
24570 mark_base_types (l);
24572 break;
24573 case dw_val_class_addr:
24574 if (a->dw_attr == DW_AT_const_value
24575 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
24577 if (AT_index (a) != NOT_INDEXED)
24578 remove_addr_table_entry (a->dw_attr_val.val_entry);
24579 remove_AT (die, a->dw_attr);
24580 ix--;
24582 if (die->die_tag == DW_TAG_GNU_call_site
24583 && a->dw_attr == DW_AT_abstract_origin)
24585 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
24586 dw_die_ref tdie = lookup_decl_die (tdecl);
24587 dw_die_ref cdie;
24588 if (tdie == NULL
24589 && DECL_EXTERNAL (tdecl)
24590 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
24591 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
24593 /* Creating a full DIE for tdecl is overly expensive and
24594 at this point even wrong when in the LTO phase
24595 as it can end up generating new type DIEs we didn't
24596 output and thus optimize_external_refs will crash. */
24597 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
24598 add_AT_flag (tdie, DW_AT_external, 1);
24599 add_AT_flag (tdie, DW_AT_declaration, 1);
24600 add_linkage_attr (tdie, tdecl);
24601 add_name_and_src_coords_attributes (tdie, tdecl);
24602 equate_decl_number_to_die (tdecl, tdie);
24604 if (tdie)
24606 a->dw_attr_val.val_class = dw_val_class_die_ref;
24607 a->dw_attr_val.v.val_die_ref.die = tdie;
24608 a->dw_attr_val.v.val_die_ref.external = 0;
24610 else
24612 if (AT_index (a) != NOT_INDEXED)
24613 remove_addr_table_entry (a->dw_attr_val.val_entry);
24614 remove_AT (die, a->dw_attr);
24615 ix--;
24618 break;
24619 default:
24620 break;
24623 FOR_EACH_CHILD (die, c, resolve_addr (c));
24626 /* Helper routines for optimize_location_lists.
24627 This pass tries to share identical local lists in .debug_loc
24628 section. */
24630 /* Iteratively hash operands of LOC opcode into HSTATE. */
24632 static void
24633 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
24635 dw_val_ref val1 = &loc->dw_loc_oprnd1;
24636 dw_val_ref val2 = &loc->dw_loc_oprnd2;
24638 switch (loc->dw_loc_opc)
24640 case DW_OP_const4u:
24641 case DW_OP_const8u:
24642 if (loc->dtprel)
24643 goto hash_addr;
24644 /* FALLTHRU */
24645 case DW_OP_const1u:
24646 case DW_OP_const1s:
24647 case DW_OP_const2u:
24648 case DW_OP_const2s:
24649 case DW_OP_const4s:
24650 case DW_OP_const8s:
24651 case DW_OP_constu:
24652 case DW_OP_consts:
24653 case DW_OP_pick:
24654 case DW_OP_plus_uconst:
24655 case DW_OP_breg0:
24656 case DW_OP_breg1:
24657 case DW_OP_breg2:
24658 case DW_OP_breg3:
24659 case DW_OP_breg4:
24660 case DW_OP_breg5:
24661 case DW_OP_breg6:
24662 case DW_OP_breg7:
24663 case DW_OP_breg8:
24664 case DW_OP_breg9:
24665 case DW_OP_breg10:
24666 case DW_OP_breg11:
24667 case DW_OP_breg12:
24668 case DW_OP_breg13:
24669 case DW_OP_breg14:
24670 case DW_OP_breg15:
24671 case DW_OP_breg16:
24672 case DW_OP_breg17:
24673 case DW_OP_breg18:
24674 case DW_OP_breg19:
24675 case DW_OP_breg20:
24676 case DW_OP_breg21:
24677 case DW_OP_breg22:
24678 case DW_OP_breg23:
24679 case DW_OP_breg24:
24680 case DW_OP_breg25:
24681 case DW_OP_breg26:
24682 case DW_OP_breg27:
24683 case DW_OP_breg28:
24684 case DW_OP_breg29:
24685 case DW_OP_breg30:
24686 case DW_OP_breg31:
24687 case DW_OP_regx:
24688 case DW_OP_fbreg:
24689 case DW_OP_piece:
24690 case DW_OP_deref_size:
24691 case DW_OP_xderef_size:
24692 hstate.add_object (val1->v.val_int);
24693 break;
24694 case DW_OP_skip:
24695 case DW_OP_bra:
24697 int offset;
24699 gcc_assert (val1->val_class == dw_val_class_loc);
24700 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
24701 hstate.add_object (offset);
24703 break;
24704 case DW_OP_implicit_value:
24705 hstate.add_object (val1->v.val_unsigned);
24706 switch (val2->val_class)
24708 case dw_val_class_const:
24709 hstate.add_object (val2->v.val_int);
24710 break;
24711 case dw_val_class_vec:
24713 unsigned int elt_size = val2->v.val_vec.elt_size;
24714 unsigned int len = val2->v.val_vec.length;
24716 hstate.add_int (elt_size);
24717 hstate.add_int (len);
24718 hstate.add (val2->v.val_vec.array, len * elt_size);
24720 break;
24721 case dw_val_class_const_double:
24722 hstate.add_object (val2->v.val_double.low);
24723 hstate.add_object (val2->v.val_double.high);
24724 break;
24725 case dw_val_class_wide_int:
24726 hstate.add (val2->v.val_wide->get_val (),
24727 get_full_len (*val2->v.val_wide)
24728 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24729 break;
24730 case dw_val_class_addr:
24731 inchash::add_rtx (val2->v.val_addr, hstate);
24732 break;
24733 default:
24734 gcc_unreachable ();
24736 break;
24737 case DW_OP_bregx:
24738 case DW_OP_bit_piece:
24739 hstate.add_object (val1->v.val_int);
24740 hstate.add_object (val2->v.val_int);
24741 break;
24742 case DW_OP_addr:
24743 hash_addr:
24744 if (loc->dtprel)
24746 unsigned char dtprel = 0xd1;
24747 hstate.add_object (dtprel);
24749 inchash::add_rtx (val1->v.val_addr, hstate);
24750 break;
24751 case DW_OP_GNU_addr_index:
24752 case DW_OP_GNU_const_index:
24754 if (loc->dtprel)
24756 unsigned char dtprel = 0xd1;
24757 hstate.add_object (dtprel);
24759 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
24761 break;
24762 case DW_OP_GNU_implicit_pointer:
24763 hstate.add_int (val2->v.val_int);
24764 break;
24765 case DW_OP_GNU_entry_value:
24766 hstate.add_object (val1->v.val_loc);
24767 break;
24768 case DW_OP_GNU_regval_type:
24769 case DW_OP_GNU_deref_type:
24771 unsigned int byte_size
24772 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
24773 unsigned int encoding
24774 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
24775 hstate.add_object (val1->v.val_int);
24776 hstate.add_object (byte_size);
24777 hstate.add_object (encoding);
24779 break;
24780 case DW_OP_GNU_convert:
24781 case DW_OP_GNU_reinterpret:
24782 if (val1->val_class == dw_val_class_unsigned_const)
24784 hstate.add_object (val1->v.val_unsigned);
24785 break;
24787 /* FALLTHRU */
24788 case DW_OP_GNU_const_type:
24790 unsigned int byte_size
24791 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
24792 unsigned int encoding
24793 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
24794 hstate.add_object (byte_size);
24795 hstate.add_object (encoding);
24796 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
24797 break;
24798 hstate.add_object (val2->val_class);
24799 switch (val2->val_class)
24801 case dw_val_class_const:
24802 hstate.add_object (val2->v.val_int);
24803 break;
24804 case dw_val_class_vec:
24806 unsigned int elt_size = val2->v.val_vec.elt_size;
24807 unsigned int len = val2->v.val_vec.length;
24809 hstate.add_object (elt_size);
24810 hstate.add_object (len);
24811 hstate.add (val2->v.val_vec.array, len * elt_size);
24813 break;
24814 case dw_val_class_const_double:
24815 hstate.add_object (val2->v.val_double.low);
24816 hstate.add_object (val2->v.val_double.high);
24817 break;
24818 case dw_val_class_wide_int:
24819 hstate.add (val2->v.val_wide->get_val (),
24820 get_full_len (*val2->v.val_wide)
24821 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24822 break;
24823 default:
24824 gcc_unreachable ();
24827 break;
24829 default:
24830 /* Other codes have no operands. */
24831 break;
24835 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
24837 static inline void
24838 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
24840 dw_loc_descr_ref l;
24841 bool sizes_computed = false;
24842 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
24843 size_of_locs (loc);
24845 for (l = loc; l != NULL; l = l->dw_loc_next)
24847 enum dwarf_location_atom opc = l->dw_loc_opc;
24848 hstate.add_object (opc);
24849 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24851 size_of_locs (loc);
24852 sizes_computed = true;
24854 hash_loc_operands (l, hstate);
24858 /* Compute hash of the whole location list LIST_HEAD. */
24860 static inline void
24861 hash_loc_list (dw_loc_list_ref list_head)
24863 dw_loc_list_ref curr = list_head;
24864 inchash::hash hstate;
24866 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24868 hstate.add (curr->begin, strlen (curr->begin) + 1);
24869 hstate.add (curr->end, strlen (curr->end) + 1);
24870 if (curr->section)
24871 hstate.add (curr->section, strlen (curr->section) + 1);
24872 hash_locs (curr->expr, hstate);
24874 list_head->hash = hstate.end ();
24877 /* Return true if X and Y opcodes have the same operands. */
24879 static inline bool
24880 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24882 dw_val_ref valx1 = &x->dw_loc_oprnd1;
24883 dw_val_ref valx2 = &x->dw_loc_oprnd2;
24884 dw_val_ref valy1 = &y->dw_loc_oprnd1;
24885 dw_val_ref valy2 = &y->dw_loc_oprnd2;
24887 switch (x->dw_loc_opc)
24889 case DW_OP_const4u:
24890 case DW_OP_const8u:
24891 if (x->dtprel)
24892 goto hash_addr;
24893 /* FALLTHRU */
24894 case DW_OP_const1u:
24895 case DW_OP_const1s:
24896 case DW_OP_const2u:
24897 case DW_OP_const2s:
24898 case DW_OP_const4s:
24899 case DW_OP_const8s:
24900 case DW_OP_constu:
24901 case DW_OP_consts:
24902 case DW_OP_pick:
24903 case DW_OP_plus_uconst:
24904 case DW_OP_breg0:
24905 case DW_OP_breg1:
24906 case DW_OP_breg2:
24907 case DW_OP_breg3:
24908 case DW_OP_breg4:
24909 case DW_OP_breg5:
24910 case DW_OP_breg6:
24911 case DW_OP_breg7:
24912 case DW_OP_breg8:
24913 case DW_OP_breg9:
24914 case DW_OP_breg10:
24915 case DW_OP_breg11:
24916 case DW_OP_breg12:
24917 case DW_OP_breg13:
24918 case DW_OP_breg14:
24919 case DW_OP_breg15:
24920 case DW_OP_breg16:
24921 case DW_OP_breg17:
24922 case DW_OP_breg18:
24923 case DW_OP_breg19:
24924 case DW_OP_breg20:
24925 case DW_OP_breg21:
24926 case DW_OP_breg22:
24927 case DW_OP_breg23:
24928 case DW_OP_breg24:
24929 case DW_OP_breg25:
24930 case DW_OP_breg26:
24931 case DW_OP_breg27:
24932 case DW_OP_breg28:
24933 case DW_OP_breg29:
24934 case DW_OP_breg30:
24935 case DW_OP_breg31:
24936 case DW_OP_regx:
24937 case DW_OP_fbreg:
24938 case DW_OP_piece:
24939 case DW_OP_deref_size:
24940 case DW_OP_xderef_size:
24941 return valx1->v.val_int == valy1->v.val_int;
24942 case DW_OP_skip:
24943 case DW_OP_bra:
24944 /* If splitting debug info, the use of DW_OP_GNU_addr_index
24945 can cause irrelevant differences in dw_loc_addr. */
24946 gcc_assert (valx1->val_class == dw_val_class_loc
24947 && valy1->val_class == dw_val_class_loc
24948 && (dwarf_split_debug_info
24949 || x->dw_loc_addr == y->dw_loc_addr));
24950 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24951 case DW_OP_implicit_value:
24952 if (valx1->v.val_unsigned != valy1->v.val_unsigned
24953 || valx2->val_class != valy2->val_class)
24954 return false;
24955 switch (valx2->val_class)
24957 case dw_val_class_const:
24958 return valx2->v.val_int == valy2->v.val_int;
24959 case dw_val_class_vec:
24960 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24961 && valx2->v.val_vec.length == valy2->v.val_vec.length
24962 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24963 valx2->v.val_vec.elt_size
24964 * valx2->v.val_vec.length) == 0;
24965 case dw_val_class_const_double:
24966 return valx2->v.val_double.low == valy2->v.val_double.low
24967 && valx2->v.val_double.high == valy2->v.val_double.high;
24968 case dw_val_class_wide_int:
24969 return *valx2->v.val_wide == *valy2->v.val_wide;
24970 case dw_val_class_addr:
24971 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24972 default:
24973 gcc_unreachable ();
24975 case DW_OP_bregx:
24976 case DW_OP_bit_piece:
24977 return valx1->v.val_int == valy1->v.val_int
24978 && valx2->v.val_int == valy2->v.val_int;
24979 case DW_OP_addr:
24980 hash_addr:
24981 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24982 case DW_OP_GNU_addr_index:
24983 case DW_OP_GNU_const_index:
24985 rtx ax1 = valx1->val_entry->addr.rtl;
24986 rtx ay1 = valy1->val_entry->addr.rtl;
24987 return rtx_equal_p (ax1, ay1);
24989 case DW_OP_GNU_implicit_pointer:
24990 return valx1->val_class == dw_val_class_die_ref
24991 && valx1->val_class == valy1->val_class
24992 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24993 && valx2->v.val_int == valy2->v.val_int;
24994 case DW_OP_GNU_entry_value:
24995 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24996 case DW_OP_GNU_const_type:
24997 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24998 || valx2->val_class != valy2->val_class)
24999 return false;
25000 switch (valx2->val_class)
25002 case dw_val_class_const:
25003 return valx2->v.val_int == valy2->v.val_int;
25004 case dw_val_class_vec:
25005 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
25006 && valx2->v.val_vec.length == valy2->v.val_vec.length
25007 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
25008 valx2->v.val_vec.elt_size
25009 * valx2->v.val_vec.length) == 0;
25010 case dw_val_class_const_double:
25011 return valx2->v.val_double.low == valy2->v.val_double.low
25012 && valx2->v.val_double.high == valy2->v.val_double.high;
25013 case dw_val_class_wide_int:
25014 return *valx2->v.val_wide == *valy2->v.val_wide;
25015 default:
25016 gcc_unreachable ();
25018 case DW_OP_GNU_regval_type:
25019 case DW_OP_GNU_deref_type:
25020 return valx1->v.val_int == valy1->v.val_int
25021 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
25022 case DW_OP_GNU_convert:
25023 case DW_OP_GNU_reinterpret:
25024 if (valx1->val_class != valy1->val_class)
25025 return false;
25026 if (valx1->val_class == dw_val_class_unsigned_const)
25027 return valx1->v.val_unsigned == valy1->v.val_unsigned;
25028 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
25029 case DW_OP_GNU_parameter_ref:
25030 return valx1->val_class == dw_val_class_die_ref
25031 && valx1->val_class == valy1->val_class
25032 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
25033 default:
25034 /* Other codes have no operands. */
25035 return true;
25039 /* Return true if DWARF location expressions X and Y are the same. */
25041 static inline bool
25042 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
25044 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
25045 if (x->dw_loc_opc != y->dw_loc_opc
25046 || x->dtprel != y->dtprel
25047 || !compare_loc_operands (x, y))
25048 break;
25049 return x == NULL && y == NULL;
25052 /* Hashtable helpers. */
25054 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
25056 static inline hashval_t hash (const dw_loc_list_struct *);
25057 static inline bool equal (const dw_loc_list_struct *,
25058 const dw_loc_list_struct *);
25061 /* Return precomputed hash of location list X. */
25063 inline hashval_t
25064 loc_list_hasher::hash (const dw_loc_list_struct *x)
25066 return x->hash;
25069 /* Return true if location lists A and B are the same. */
25071 inline bool
25072 loc_list_hasher::equal (const dw_loc_list_struct *a,
25073 const dw_loc_list_struct *b)
25075 if (a == b)
25076 return 1;
25077 if (a->hash != b->hash)
25078 return 0;
25079 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
25080 if (strcmp (a->begin, b->begin) != 0
25081 || strcmp (a->end, b->end) != 0
25082 || (a->section == NULL) != (b->section == NULL)
25083 || (a->section && strcmp (a->section, b->section) != 0)
25084 || !compare_locs (a->expr, b->expr))
25085 break;
25086 return a == NULL && b == NULL;
25089 typedef hash_table<loc_list_hasher> loc_list_hash_type;
25092 /* Recursively optimize location lists referenced from DIE
25093 children and share them whenever possible. */
25095 static void
25096 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
25098 dw_die_ref c;
25099 dw_attr_node *a;
25100 unsigned ix;
25101 dw_loc_list_struct **slot;
25103 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25104 if (AT_class (a) == dw_val_class_loc_list)
25106 dw_loc_list_ref list = AT_loc_list (a);
25107 /* TODO: perform some optimizations here, before hashing
25108 it and storing into the hash table. */
25109 hash_loc_list (list);
25110 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
25111 if (*slot == NULL)
25112 *slot = list;
25113 else
25114 a->dw_attr_val.v.val_loc_list = *slot;
25117 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
25121 /* Recursively assign each location list a unique index into the debug_addr
25122 section. */
25124 static void
25125 index_location_lists (dw_die_ref die)
25127 dw_die_ref c;
25128 dw_attr_node *a;
25129 unsigned ix;
25131 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25132 if (AT_class (a) == dw_val_class_loc_list)
25134 dw_loc_list_ref list = AT_loc_list (a);
25135 dw_loc_list_ref curr;
25136 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
25138 /* Don't index an entry that has already been indexed
25139 or won't be output. */
25140 if (curr->begin_entry != NULL
25141 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
25142 continue;
25144 curr->begin_entry
25145 = add_addr_table_entry (xstrdup (curr->begin),
25146 ate_kind_label);
25150 FOR_EACH_CHILD (die, c, index_location_lists (c));
25153 /* Optimize location lists referenced from DIE
25154 children and share them whenever possible. */
25156 static void
25157 optimize_location_lists (dw_die_ref die)
25159 loc_list_hash_type htab (500);
25160 optimize_location_lists_1 (die, &htab);
25163 /* Traverse the limbo die list, and add parent/child links. The only
25164 dies without parents that should be here are concrete instances of
25165 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
25166 For concrete instances, we can get the parent die from the abstract
25167 instance. */
25169 static void
25170 flush_limbo_die_list (void)
25172 limbo_die_node *node, *next_node;
25174 for (node = limbo_die_list; node; node = next_node)
25176 dw_die_ref die = node->die;
25177 next_node = node->next;
25179 if (die->die_parent == NULL)
25181 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
25183 if (origin && origin->die_parent)
25184 add_child_die (origin->die_parent, die);
25185 else if (is_cu_die (die))
25187 else if (seen_error ())
25188 /* It's OK to be confused by errors in the input. */
25189 add_child_die (comp_unit_die (), die);
25190 else
25192 /* In certain situations, the lexical block containing a
25193 nested function can be optimized away, which results
25194 in the nested function die being orphaned. Likewise
25195 with the return type of that nested function. Force
25196 this to be a child of the containing function.
25198 It may happen that even the containing function got fully
25199 inlined and optimized out. In that case we are lost and
25200 assign the empty child. This should not be big issue as
25201 the function is likely unreachable too. */
25202 gcc_assert (node->created_for);
25204 if (DECL_P (node->created_for))
25205 origin = get_context_die (DECL_CONTEXT (node->created_for));
25206 else if (TYPE_P (node->created_for))
25207 origin = scope_die_for (node->created_for, comp_unit_die ());
25208 else
25209 origin = comp_unit_die ();
25211 add_child_die (origin, die);
25216 limbo_die_list = NULL;
25219 /* Output stuff that dwarf requires at the end of every file,
25220 and generate the DWARF-2 debugging info. */
25222 static void
25223 dwarf2out_finish (const char *filename)
25225 comdat_type_node *ctnode;
25226 dw_die_ref main_comp_unit_die;
25228 /* Flush out any latecomers to the limbo party. */
25229 flush_limbo_die_list ();
25231 /* We shouldn't have any symbols with delayed asm names for
25232 DIEs generated after early finish. */
25233 gcc_assert (deferred_asm_name == NULL);
25235 /* PCH might result in DW_AT_producer string being restored from the
25236 header compilation, so always fill it with empty string initially
25237 and overwrite only here. */
25238 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
25239 producer_string = gen_producer_string ();
25240 producer->dw_attr_val.v.val_str->refcount--;
25241 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
25243 gen_remaining_tmpl_value_param_die_attribute ();
25245 /* Add the name for the main input file now. We delayed this from
25246 dwarf2out_init to avoid complications with PCH.
25247 For LTO produced units use a fixed artificial name to avoid
25248 leaking tempfile names into the dwarf. */
25249 if (!in_lto_p)
25250 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
25251 else
25252 add_name_attribute (comp_unit_die (), "<artificial>");
25253 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
25254 add_comp_dir_attribute (comp_unit_die ());
25255 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
25257 bool p = false;
25258 file_table->traverse<bool *, file_table_relative_p> (&p);
25259 if (p)
25260 add_comp_dir_attribute (comp_unit_die ());
25263 #if ENABLE_ASSERT_CHECKING
25265 dw_die_ref die = comp_unit_die (), c;
25266 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
25268 #endif
25269 resolve_addr (comp_unit_die ());
25270 move_marked_base_types ();
25272 /* Walk through the list of incomplete types again, trying once more to
25273 emit full debugging info for them. */
25274 retry_incomplete_types ();
25276 if (flag_eliminate_unused_debug_types)
25277 prune_unused_types ();
25279 /* Generate separate COMDAT sections for type DIEs. */
25280 if (use_debug_types)
25282 break_out_comdat_types (comp_unit_die ());
25284 /* Each new type_unit DIE was added to the limbo die list when created.
25285 Since these have all been added to comdat_type_list, clear the
25286 limbo die list. */
25287 limbo_die_list = NULL;
25289 /* For each new comdat type unit, copy declarations for incomplete
25290 types to make the new unit self-contained (i.e., no direct
25291 references to the main compile unit). */
25292 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25293 copy_decls_for_unworthy_types (ctnode->root_die);
25294 copy_decls_for_unworthy_types (comp_unit_die ());
25296 /* In the process of copying declarations from one unit to another,
25297 we may have left some declarations behind that are no longer
25298 referenced. Prune them. */
25299 prune_unused_types ();
25302 /* Generate separate CUs for each of the include files we've seen.
25303 They will go into limbo_die_list. */
25304 if (flag_eliminate_dwarf2_dups)
25305 break_out_includes (comp_unit_die ());
25307 /* Traverse the DIE's and add sibling attributes to those DIE's that
25308 have children. */
25309 add_sibling_attributes (comp_unit_die ());
25310 limbo_die_node *node;
25311 for (node = limbo_die_list; node; node = node->next)
25312 add_sibling_attributes (node->die);
25313 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25314 add_sibling_attributes (ctnode->root_die);
25316 /* When splitting DWARF info, we put some attributes in the
25317 skeleton compile_unit DIE that remains in the .o, while
25318 most attributes go in the DWO compile_unit_die. */
25319 if (dwarf_split_debug_info)
25320 main_comp_unit_die = gen_compile_unit_die (NULL);
25321 else
25322 main_comp_unit_die = comp_unit_die ();
25324 /* Output a terminator label for the .text section. */
25325 switch_to_section (text_section);
25326 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
25327 if (cold_text_section)
25329 switch_to_section (cold_text_section);
25330 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
25333 /* We can only use the low/high_pc attributes if all of the code was
25334 in .text. */
25335 if (!have_multiple_function_sections
25336 || (dwarf_version < 3 && dwarf_strict))
25338 /* Don't add if the CU has no associated code. */
25339 if (text_section_used)
25340 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
25341 text_end_label, true);
25343 else
25345 unsigned fde_idx;
25346 dw_fde_ref fde;
25347 bool range_list_added = false;
25349 if (text_section_used)
25350 add_ranges_by_labels (main_comp_unit_die, text_section_label,
25351 text_end_label, &range_list_added, true);
25352 if (cold_text_section_used)
25353 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
25354 cold_end_label, &range_list_added, true);
25356 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
25358 if (DECL_IGNORED_P (fde->decl))
25359 continue;
25360 if (!fde->in_std_section)
25361 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
25362 fde->dw_fde_end, &range_list_added,
25363 true);
25364 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
25365 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
25366 fde->dw_fde_second_end, &range_list_added,
25367 true);
25370 if (range_list_added)
25372 /* We need to give .debug_loc and .debug_ranges an appropriate
25373 "base address". Use zero so that these addresses become
25374 absolute. Historically, we've emitted the unexpected
25375 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
25376 Emit both to give time for other tools to adapt. */
25377 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
25378 if (! dwarf_strict && dwarf_version < 4)
25379 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
25381 add_ranges (NULL);
25385 if (debug_info_level >= DINFO_LEVEL_TERSE)
25386 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
25387 debug_line_section_label);
25389 if (have_macinfo)
25390 add_AT_macptr (comp_unit_die (),
25391 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
25392 macinfo_section_label);
25394 if (dwarf_split_debug_info)
25396 /* optimize_location_lists calculates the size of the lists,
25397 so index them first, and assign indices to the entries.
25398 Although optimize_location_lists will remove entries from
25399 the table, it only does so for duplicates, and therefore
25400 only reduces ref_counts to 1. */
25401 index_location_lists (comp_unit_die ());
25403 if (addr_index_table != NULL)
25405 unsigned int index = 0;
25406 addr_index_table
25407 ->traverse_noresize<unsigned int *, index_addr_table_entry>
25408 (&index);
25412 if (have_location_lists)
25413 optimize_location_lists (comp_unit_die ());
25415 save_macinfo_strings ();
25417 if (dwarf_split_debug_info)
25419 unsigned int index = 0;
25421 /* Add attributes common to skeleton compile_units and
25422 type_units. Because these attributes include strings, it
25423 must be done before freezing the string table. Top-level
25424 skeleton die attrs are added when the skeleton type unit is
25425 created, so ensure it is created by this point. */
25426 add_top_level_skeleton_die_attrs (main_comp_unit_die);
25427 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
25430 /* Output all of the compilation units. We put the main one last so that
25431 the offsets are available to output_pubnames. */
25432 for (node = limbo_die_list; node; node = node->next)
25433 output_comp_unit (node->die, 0);
25435 hash_table<comdat_type_hasher> comdat_type_table (100);
25436 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25438 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
25440 /* Don't output duplicate types. */
25441 if (*slot != HTAB_EMPTY_ENTRY)
25442 continue;
25444 /* Add a pointer to the line table for the main compilation unit
25445 so that the debugger can make sense of DW_AT_decl_file
25446 attributes. */
25447 if (debug_info_level >= DINFO_LEVEL_TERSE)
25448 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
25449 (!dwarf_split_debug_info
25450 ? debug_line_section_label
25451 : debug_skeleton_line_section_label));
25453 output_comdat_type_unit (ctnode);
25454 *slot = ctnode;
25457 /* The AT_pubnames attribute needs to go in all skeleton dies, including
25458 both the main_cu and all skeleton TUs. Making this call unconditional
25459 would end up either adding a second copy of the AT_pubnames attribute, or
25460 requiring a special case in add_top_level_skeleton_die_attrs. */
25461 if (!dwarf_split_debug_info)
25462 add_AT_pubnames (comp_unit_die ());
25464 if (dwarf_split_debug_info)
25466 int mark;
25467 unsigned char checksum[16];
25468 struct md5_ctx ctx;
25470 /* Compute a checksum of the comp_unit to use as the dwo_id. */
25471 md5_init_ctx (&ctx);
25472 mark = 0;
25473 die_checksum (comp_unit_die (), &ctx, &mark);
25474 unmark_all_dies (comp_unit_die ());
25475 md5_finish_ctx (&ctx, checksum);
25477 /* Use the first 8 bytes of the checksum as the dwo_id,
25478 and add it to both comp-unit DIEs. */
25479 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
25480 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
25482 /* Add the base offset of the ranges table to the skeleton
25483 comp-unit DIE. */
25484 if (ranges_table_in_use)
25485 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
25486 ranges_section_label);
25488 switch_to_section (debug_addr_section);
25489 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
25490 output_addr_table ();
25493 /* Output the main compilation unit if non-empty or if .debug_macinfo
25494 or .debug_macro will be emitted. */
25495 output_comp_unit (comp_unit_die (), have_macinfo);
25497 if (dwarf_split_debug_info && info_section_emitted)
25498 output_skeleton_debug_sections (main_comp_unit_die);
25500 /* Output the abbreviation table. */
25501 if (abbrev_die_table_in_use != 1)
25503 switch_to_section (debug_abbrev_section);
25504 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
25505 output_abbrev_section ();
25508 /* Output location list section if necessary. */
25509 if (have_location_lists)
25511 /* Output the location lists info. */
25512 switch_to_section (debug_loc_section);
25513 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
25514 output_location_lists (comp_unit_die ());
25517 output_pubtables ();
25519 /* Output the address range information if a CU (.debug_info section)
25520 was emitted. We output an empty table even if we had no functions
25521 to put in it. This because the consumer has no way to tell the
25522 difference between an empty table that we omitted and failure to
25523 generate a table that would have contained data. */
25524 if (info_section_emitted)
25526 switch_to_section (debug_aranges_section);
25527 output_aranges ();
25530 /* Output ranges section if necessary. */
25531 if (ranges_table_in_use)
25533 switch_to_section (debug_ranges_section);
25534 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
25535 output_ranges ();
25538 /* Have to end the macro section. */
25539 if (have_macinfo)
25541 switch_to_section (debug_macinfo_section);
25542 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
25543 output_macinfo ();
25544 dw2_asm_output_data (1, 0, "End compilation unit");
25547 /* Output the source line correspondence table. We must do this
25548 even if there is no line information. Otherwise, on an empty
25549 translation unit, we will generate a present, but empty,
25550 .debug_info section. IRIX 6.5 `nm' will then complain when
25551 examining the file. This is done late so that any filenames
25552 used by the debug_info section are marked as 'used'. */
25553 switch_to_section (debug_line_section);
25554 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
25555 if (! DWARF2_ASM_LINE_DEBUG_INFO)
25556 output_line_info (false);
25558 if (dwarf_split_debug_info && info_section_emitted)
25560 switch_to_section (debug_skeleton_line_section);
25561 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
25562 output_line_info (true);
25565 /* If we emitted any indirect strings, output the string table too. */
25566 if (debug_str_hash || skeleton_debug_str_hash)
25567 output_indirect_strings ();
25570 /* Perform any cleanups needed after the early debug generation pass
25571 has run. */
25573 static void
25574 dwarf2out_early_finish (void)
25576 limbo_die_node *node;
25578 /* Add DW_AT_linkage_name for all deferred DIEs. */
25579 for (node = deferred_asm_name; node; node = node->next)
25581 tree decl = node->created_for;
25582 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
25583 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
25584 ended up in deferred_asm_name before we knew it was
25585 constant and never written to disk. */
25586 && DECL_ASSEMBLER_NAME (decl))
25588 add_linkage_attr (node->die, decl);
25589 move_linkage_attr (node->die);
25592 deferred_asm_name = NULL;
25594 /* The point here is to flush out the limbo list so that it is empty
25595 and we don't need to stream it for LTO. */
25596 flush_limbo_die_list ();
25598 gen_scheduled_generic_parms_dies ();
25599 gen_remaining_tmpl_value_param_die_attribute ();
25602 /* Reset all state within dwarf2out.c so that we can rerun the compiler
25603 within the same process. For use by toplev::finalize. */
25605 void
25606 dwarf2out_c_finalize (void)
25608 last_var_location_insn = NULL;
25609 cached_next_real_insn = NULL;
25610 used_rtx_array = NULL;
25611 incomplete_types = NULL;
25612 decl_scope_table = NULL;
25613 debug_info_section = NULL;
25614 debug_skeleton_info_section = NULL;
25615 debug_abbrev_section = NULL;
25616 debug_skeleton_abbrev_section = NULL;
25617 debug_aranges_section = NULL;
25618 debug_addr_section = NULL;
25619 debug_macinfo_section = NULL;
25620 debug_line_section = NULL;
25621 debug_skeleton_line_section = NULL;
25622 debug_loc_section = NULL;
25623 debug_pubnames_section = NULL;
25624 debug_pubtypes_section = NULL;
25625 debug_str_section = NULL;
25626 debug_str_dwo_section = NULL;
25627 debug_str_offsets_section = NULL;
25628 debug_ranges_section = NULL;
25629 debug_frame_section = NULL;
25630 fde_vec = NULL;
25631 debug_str_hash = NULL;
25632 skeleton_debug_str_hash = NULL;
25633 dw2_string_counter = 0;
25634 have_multiple_function_sections = false;
25635 text_section_used = false;
25636 cold_text_section_used = false;
25637 cold_text_section = NULL;
25638 current_unit_personality = NULL;
25640 next_die_offset = 0;
25641 single_comp_unit_die = NULL;
25642 comdat_type_list = NULL;
25643 limbo_die_list = NULL;
25644 file_table = NULL;
25645 decl_die_table = NULL;
25646 common_block_die_table = NULL;
25647 decl_loc_table = NULL;
25648 call_arg_locations = NULL;
25649 call_arg_loc_last = NULL;
25650 call_site_count = -1;
25651 tail_call_site_count = -1;
25652 cached_dw_loc_list_table = NULL;
25653 abbrev_die_table = NULL;
25654 abbrev_die_table_allocated = 0;
25655 abbrev_die_table_in_use = 0;
25656 line_info_label_num = 0;
25657 cur_line_info_table = NULL;
25658 text_section_line_info = NULL;
25659 cold_text_section_line_info = NULL;
25660 separate_line_info = NULL;
25661 info_section_emitted = false;
25662 pubname_table = NULL;
25663 pubtype_table = NULL;
25664 macinfo_table = NULL;
25665 ranges_table = NULL;
25666 ranges_table_allocated = 0;
25667 ranges_table_in_use = 0;
25668 ranges_by_label = 0;
25669 ranges_by_label_allocated = 0;
25670 ranges_by_label_in_use = 0;
25671 have_location_lists = false;
25672 loclabel_num = 0;
25673 poc_label_num = 0;
25674 last_emitted_file = NULL;
25675 label_num = 0;
25676 tmpl_value_parm_die_table = NULL;
25677 generic_type_instances = NULL;
25678 frame_pointer_fb_offset = 0;
25679 frame_pointer_fb_offset_valid = false;
25680 base_types.release ();
25681 XDELETEVEC (producer_string);
25682 producer_string = NULL;
25685 #include "gt-dwarf2out.h"